javascript - Can we store an array of objects in a script parameter? - Stack Overflow

admin2025-05-01  0

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;
    } 

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;
    } 
Share Improve this question edited Jan 4 at 7:34 Maira S asked Jan 2 at 17:05 Maira SMaira S 857 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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.

Since my saved search is sorted in ascending order, failed items stay at the top. To skip them after rescheduling, I use a script parameter (faileditemcount) to track the number of failed items and adjust the saved search start and end indices accordingly.

Updated Function: var getMoreRecords = function(transactionSearchObj, failedjecount) { var startPos = faileditemcount; var endPos = 50 + faileditemcount; var resultSet = transactionSearchObj.run(); return resultSet.getRange({ start: startPos, end: endPos }) || []; };

Script Logic:

  1. Fetch failedjecount from the script parameter.

  2. Skip failed items using failedjecount in the search.

  3. Process records and count new failed items.

  4. Update failedjecount and reschedule.

This approach skips failed items, tracks them efficiently, and avoids infinite rescheduling.

转载请注明原文地址:http://anycun.com/QandA/1746105902a91752.html