编程语言
首页 > 编程语言> > javascript-通过分页和日期范围过滤在Netsuite SuiteScript 2.0中实现预先加载

javascript-通过分页和日期范围过滤在Netsuite SuiteScript 2.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条记录.

有谁知道如何通过针对日期/时间范围对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;
});

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

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

标签:netsuite,suitescript,javascript,suitescript2-0
来源: https://codeday.me/bug/20191025/1925358.html