【问题标题】:Implement eager loading in Netsuite SuiteScript 2.0, with pagination and date range filtering在 Netsuite SuiteScript 2.0 中实现预先加载,带有分页和日期范围过滤
【发布时间】:2019-01-22 05:56:06
【问题描述】:

我想(渴望)加载 netsuite 中已在 2 个日期/时间范围内更新的客户列表,并将结果分页。

我对 NetSuite SuiteScript 2.0 比较陌生,所以我实现了可以工作的延迟加载 mvp 版本(没有过滤),它看起来像这样:

define(['N/record', 'N/search'], function(record, search) {

    function loadClients(context) {

        var currencyMap = {};
        var statusMap = {};

        var results = [];

        search.create({

            type: search.Type.CUSTOMER,
            // todo: Workout how to apply filter to load only customers updated between two date ranges (supplied via context) using 'lastmodifieddate'

        }).run().getRange({

            start: context.start || 0,
            end: context.end || 100,

        }).forEach(function(result) {

            var customer = loadCustomer(result.id);

            var currencyId = customer.getValue({ fieldId: 'currency' });
            if (typeof currencyMap[currencyId] === 'undefined') {
                currencyMap[currencyId] = loadCurrency(currencyId).getValue({ 
                    fieldId: 'name'
                });
            }

            var statusId = customer.getValue({ fieldId: 'entitystatus' });
            if (typeof statusMap[statusId] === 'undefined') {
                statusMap[statusId] = loadStatus(statusId).getValue({
                    fieldId: 'name'
                });
            }

            results.push({
                tax_number: customer.getValue({ fieldId: 'vatregnumber' }),
                name: customer.getValue({ fieldId: 'companyname' }),
                first_name: '',
                last_name: '',
                updated_date: customer.getValue({ fieldId: 'lastmodifieddate' }),
                has_attachments: '',
                default_currency: currencyMap[currencyId],
                is_supplier: 0,
                contact_id: customer.id,
                email_address: customer.getValue({ fieldId: 'email' }),
                phones: customer.getValue({ fieldId: 'phone' }),
                is_customer: 1,
                addresses: customer.getValue({ fieldId: 'defaultaddress' }),
                contact_status: statusMap[statusId],
            });

        });

        return results;

    }

    function loadCustomer(customerId) {
        return record.load({
            type: record.Type.CUSTOMER,
            id: customerId,
            isDynamic: false
        });
    }

    function loadCurrency(currencyId) {
        return record.load({
            type: record.Type.CURRENCY,
            id: currencyId,
            isDynamic: false
        });
    }

    function loadStatus(statusId) {
        return record.load({
            type: record.Type.CUSTOMER_STATUS,
            id: statusId,
            isDynamic: false
        });
    }

    return {
        post: loadClients
    }

});

如您所见,由于对它的工作原理缺乏了解,我的数据加载效率非常低,而且速度非常慢。加载 100 条记录大约需要 1 分钟。

有谁知道如何通过在lastmodifieddate 上过滤日期/时间范围并正确加载来实现上述目标?

【问题讨论】:

    标签: javascript netsuite suitescript suitescript2.0


    【解决方案1】:

    这里的主要问题是您要单独加载每个完整的客户记录。您实际上不太可能需要这样做。我建议的方法是在搜索列中包含您需要的结果。比如:

    var results = [];
    search.create({
        type: search.Type.CUSTOMER,
        filters: [['lastmodifieddate', 'within', '1/1/2018', '2/1/2018']],
        columns: ['vatregnumber','companyname', 'lastmodifieddate', /*ETC*/ ]
    }).run().each(function(result) {
        results.push({
            tax_number: result.getValue('vatregnumber'),
            name: result.getValue('companyname'),
            updated_date: result.getValue('lastmodifieddate')
        });
        return true;
    });
    

    要动态创建过滤器,您必须在帖子正文 ({startDate: '1/1/2018', endDate: 2/1/2018}) 中将开始和结束日期作为参数传递,并在过滤器中使用它们,例如:

    filters: [['lastmodifieddate', 'within', context.startDate, context.endDate]]
    

    【讨论】:

    • 嘿,非常感谢这种方法。客户可能有一个或多个联系人。如果我想在客户记录中加载默认联系人(例如,我可以获取他们的名字/姓氏)怎么办?您将如何在搜索列条件中定义它,然后通过getValue() 获取值?
    • 没关系;经过一些实验,我已经弄清楚了。如果我在搜索栏中输入contact.firstname,它会急切地加载关系(客户-> 联系人)并在客户的主要联系人上,获取名字并将其返回到字段(在结果对象上),如下所示:@ 987654326@ - 呸!这很容易。
    • 其实忽略了我之前评论的最后一部分。我不知道如何在结果对象上使用getValue() 方法检索此值。 result.getValue('contact.firstname') 返回 null,但如果我返回整个结果对象,我可以在那里看到字段/值。
    • 其实没关系,经过更多的修补;找到了答案:result.getValue({ name: 'firstname', join: 'contact' }); 我需要这个来检索急切加载的关系数据值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多