Say I have a saved search with a list of line item...
# suitescript
j
Say I have a saved search with a list of line items on purchase orders, how do I group the lines for the same purchase orders and set sublist values for each line in one go rather than load, set sublist, save, load, set sublist, save for the same PO? Current Script:
Copy code
define(["N/record", "N/search"],

    function (record, search) {

        function execute(scriptContext) {

            const searchObj = search.load({ id: "customsearch_no_3_way_match" }) //load the saved search 

            searchObj.run().each(function (result) {
                const poID = result.getValue(result.columns[2]);
                const poLN = result.getValue(result.columns[5]);
                const item = result.getText(result.columns[4]);
                const poRate = Number(result.getValue(result.columns[6]));
                const irID = result.getValue(result.columns[8]);
                const irLN = result.getValue(result.columns[11]);
                const irRate = Number(result.getValue(result.columns[12]));
                const bID = result.getValue(result.columns[14]);
                const bLN = result.getValue(result.columns[17]);
                const bRate = Number(result.getValue(result.columns[18]));
                log.debug({
                    title: poID,
                    details: "Item: " + item + " | poID: " + poID + " | poLN: " + poLN + " | poRate: " + poRate + " | irID: " + irID + " | irLN: " + irLN + " | irRate: " + irRate + " | bID: " + bID + " | bLN: " + bLN + " | bRate: " + bRate
                });
                try {
                    if (bRate != poRate) {
                        log.debug({
                            title: poID,
                            details: 'bRate != poRate'
                        });
                        const po = record.load({
                            type: record.Type.PURCHASE_ORDER,
                            id: poID
                        })
                        po.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'rate',
                            line: (poLN -1),
                            value: bRate
                        });
                        po.save({
                            enableSourcing: true,
                            ignoreMandatoryFields: true
                        });
                    }
                    if (bRate != irRate) {
                        log.debug({
                            title: irID,
                            details: 'bRate != irRate'
                        });
                        const ir = record.load({
                            type: record.Type.ITEM_RECEIPT,
                            id: irID
                        })
                        ir.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'rate',
                            line: (irLN - 1),
                            value: bRate
                        });
                        ir.save({
                            enableSourcing: true,
                            ignoreMandatoryFields: true
                        });
                    }
                }
                catch (e) {
                    log.error({
                        title: poID,
                        details: e
                    });
                }
                return true;
            });

        }

        return {
            execute: execute
        };

    });
s
You could do it as a map reduce script, the reduce stage could consolidate all the changes for a given PO into one action
e
Do the above or create your own grouped objects from the search results.
r
Populate an object like this.