简体   繁体   中英

SuiteScript Updating Search Result Fields

I have a search in SuiteScript 2.0 that's working fine. But for each record the search brings back, I want to update a particular field (I use it elsewhere to determine that the record has been examined). It's potentially a very large result set, so I have to page it. Here's my code:

var sResult = mySearch.runPaged({pageSize: 10});
for (var pageIndex = 0; pageIndex < sResult.pageRanges.length; pageIndex++) 
{
    var searchPage = sResult.fetch({ index: pageRange.index });

    searchPage.data.forEach(function (result) 
    {
        var name = result.getValue({ name: "altname"})
        
        result.setValue({
                          name: 'tracker',
                          value: new Date()
        })
    });
}             

You can see where I have a call to result.setValue() , which is a non-existent function. But it shows where I want to update the 'tracker' field and what data I want to use.

Am I barking up the wrong tree entirely here? How do I update the field for each result returned?

As Simon says you can't directly update a search result, but you can use submitFields method.

This example is from NetSuite documentation:

var otherId = record.submitFields({ 
    type: 'customrecord_book', //record Type
    id: '4', // record Id
    values: {
        'custrecord_rating': '2'
    }
});

This approach will save more governance than load and save the record.

AFAIK You can't directly update a search result and write the value back to the record, the record needs to be loaded first. The snippet doesn't say what the type of record it is you're searching for or want to load, but the general idea is (in place of your result.setValue line):

var loadedRecord = Record.load({type:'myrecordtype', id:result.id});
loadedRecord.setValue( {fieldId:'tracker', value: new Date() });
loadedRecord.save();

Keep in mind SuiteScript governance and the number of records your modifying. Load & Save too many and your script will terminate earlier than you expect.

Bad design: instead of using result.setValue inside the iteration, push those to an "update" array then after the data.forEach have another function that loops thru the update array and processes them there with record.submitFields()

Be careful of governance....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM