Netsuite SuiteScript 2.1记录更新太慢

Netsuite SuiteScript 2.1记录更新太慢,netsuite,suitescript,suitescript2.0,Netsuite,Suitescript,Suitescript2.0,我有一个我编写的afterSubmit函数,它将迭代所有与相同CTG_ID值(这是一个自定义父记录)相关的事务,脚本实际上只更新所有这些值上的一个字段 我的问题是,这是一种非常慢的方法,我连接到同一父级的事务越多,用户在单击“保存”按钮后需要等待的时间就越多。脚本执行时间很糟糕 有没有更快/更好的方法来更新一组记录上的某个字段 我更新这些交易记录的功能: function afterSubmit(context) { const newRecord = context.newRe

我有一个我编写的afterSubmit函数,它将迭代所有与相同CTG_ID值(这是一个自定义父记录)相关的事务,脚本实际上只更新所有这些值上的一个字段

我的问题是,这是一种非常慢的方法,我连接到同一父级的事务越多,用户在单击“保存”按钮后需要等待的时间就越多。脚本执行时间很糟糕

有没有更快/更好的方法来更新一组记录上的某个字段

我更新这些交易记录的功能:

function afterSubmit(context) {
        const newRecord = context.newRecord;
        const ctgId = newRecord.getValue({ fieldId: 'custbody_ctgid' });
        const currentCustomerPo = newRecord.getValue({ fieldId: 'custbodyctg_customer_po'})

        search.create({
            type: 'transaction',
            filters: [['custbody_ctgid', 'anyof', ctgId],
                "AND",
                ['mainline','is','T']],
            columns: ['custbodyctg_customer_po']
        }).run().getRange({start: 0, end:100}).forEach((result,line) => {
            const ctgPo = result.getValue('custbodyctg_customer_po')        as string;
            const recType = result.recordType;
            const recId = result.id;
            let rec = record.load({
                type: recType,
                id: recId,
                isDynamic: true
            })
            rec.setValue({
                fieldId: 'custbodyctg_customer_po',
                value: currentCustomerPo,
                ignoreFieldChange: true
            })
            rec.save();
        })
    }
多亏了Brian Duffy的回答,这项工作做得好多了

我修改了脚本,因此现在我使用
每个
迭代结果,而不是使用
forEach
函数。我正在使用
record.submitFields
函数,而不是
record.load

function afterSubmit(context) {
        const newRecord = context.newRecord;
        const oldRecord = context.oldRecord;
        const ctgId = newRecord.getValue({fieldId: 'custbody_ctgid'});
        const currentCustomerPo = newRecord.getValue({fieldId: 'custbodyctg_customer_po'})
        const oldCustomerPo = oldRecord.getValue({fieldId: 'custbodyctg_customer_po'})
        if (oldCustomerPo !== currentCustomerPo) {
            search.create({
                type: 'transaction',
                filters: [['custbody_ctgid', 'anyof', ctgId],
                    "AND",
                    ['mainline', 'is', 'T']],
                columns: ['custbodyctg_customer_po', 'type']
            }).run().each((result) => {
                if (result.recordType !== 'salesorder') {
                    const recType   = result.recordType;
                    const recId     = result.id;

                    record.submitFields({
                        type: recType,
                        id: recId,
                        values: {
                            custbodyctg_customer_po: currentCustomerPo
                        },
                        options: {
                            enableSourcing: false,
                            ignoreMandatoryFields: true
                        }
                    });
                }
                return true;
            })
        }
    }

尽管如此,在测试了几个平均有5个链接事务的事务之后,这运行了5-7秒。对我来说还是慢一点。如果有人有建议,那就太棒了

我会尝试在搜索结果中使用run.each而不是getRange,并使用record.sumbitFields而不是加载和保存记录。

我尝试过,但速度仍然很慢。还有其他更快的方法吗?