skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search