在 Netsuite SuiteScript 2.0 中实现预先加载,具有分页和日期范围过滤

Implement eager loading in Netsuite SuiteScript 2.0, with pagination and date range filtering

我想(急切地)加载已在 2 date/time 范围内更新的 netsuite 中的客户列表,并对结果进行分页。

我是 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 分钟

有谁知道如何通过对 date/time 范围的 lastmodifieddate 进行过滤并正确预加载来实现上述目标?

这里的主要问题是您要单独加载每条完整的客户记录。您实际上不太可能需要这样做。相反,我建议的方法是将您需要的结果包含在搜索栏中。类似于:

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;
});

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

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