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

时间:2018-08-15 10:47:14

标签: javascript netsuite suitescript suitescript2.0

我想(急切)在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
    }

});

如您所见,由于缺乏有关其工作原理的知识,我正在执行非常低效的数据加载,而且速度非常慢。大约需要1分钟来加载 100条记录

有人知道如何通过对{/ {1}}进行过滤以实现日期/时间范围并渴望正确加载吗?

1 个答案:

答案 0 :(得分:4)

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

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]]