javascriptparameterstasknetsuitesuitescript2.0

Can we store an array of objects in a script parameter?


I have created a scheduled script to process invoice items. I process the items from a saved search in batches (from start: 0 to end: 50 items) and then reschedule the script. After processing (creating records for each item), the successful items are removed from the saved search, but some items may fail due to incorrect data. I store the failed items in an array. However, after rescheduling, that array becomes empty. When the script runs again, I retrieve both the failed items and the next set of items from the saved search, causing the script to process the failed items again.

If all items fail during any reschedule, the script will continuously reschedule infinitely.

To address this, I have created a long text script parameter to store the failed item IDs and line numbers. However, I understand that this approach is not ideal. I would appreciate any advice on a better solution.

Note: I'm sorting saved search result in ascending order by invoice internal id.

This is my saved search function,

var getMoreRecords = function 
(transactionSearchObj, 
startPosition, endPosition) {
        var startPos = 0;
        var endPos = 50; // Only 
fetch up to 50 records
        var allRecords = new 
Array();

        var resultSet = 
transactionSearchObj.run();
        try {
            var currList = 
resultSet.getRange({
                start: startPos,
                end: endPos
            });
        } catch (ex) {
            // log.debug('Checking', ' error --> ' + ex);
            return allRecords;
        }

        if (currList && 
currList.length > 0) {
            allRecords = currList;
 // Assign the first 50 records
        }

        return allRecords;
    } 

Solution

  • Yes.

    1. Write the Array to the parameter as a JSON string using JSON.stringify().
    2. Parse the JSON string from the parameter back to an Array using JSON.parse().

    If you need more data than the parameter can hold, then you might instead write your failed IDs to a file, and pass the file ID in the parameter instead. If you go this route, you will want to have a regular process for cleaning up the generated files once they're no longer needed.