restartAction()
The restartAction() function (server-side) is used when processing server-side operations in batches to avoid exceeding governance usage unit limits. When verifying the usage units left, include this function to:
-
Stop the current server-side action.
-
Send information to the client about whether the server-side action should be restarted.
To use restartAction(), create a loop in a server-side action to handle each record. Store both the array of records and the current index in the scratchpad variable so progress is tracked between calls. If you need to retrieve or prepare the records in the server-side action, do this outside the loop and only on the first call of the action. During each loop iteration, verify the available governance units left using getRemainingUnits(). When the server-side action is about to reach the limit, call restartAction() to stop processing and let the client restart it later.
Syntax
Use this syntax to call the restartAction() function in a server-side action:
restartAction();
Return Value
The restartAction() function returns undefined.
Parameters
This function doesn't require any parameters. Its purpose is to stop the processing of server operations and notify the client of the server-side action's status.
Examples
The following examples show how to use the restartAction() function.
Retrieving and Processing Records in Batches
In this example, records are created one at a time in a loop. After every operation, the script verifies the remaining usage units before processing a new record. If there aren't enough usage units left to process the next record, restartAction() is called to pause the server-side script and send information back to the client about its completion status. The client can then restart the process to handle any remaining records. For more information, see Performing Server Requests in Batches.
// Server-side action
let records = scratchpad.records,
count = records.length,
weight = 0;
do {
let remaining = getRemainingUnits();
if (remaining < weight) {
let unprocessed = count - scratchpad.index; // Records to be created on the following requests
console.log('Operation completed. Records left:', unprocessed);
restartAction();
}
let record = records[scratchpad.index];
createRecord(record).done((data) => {
scratchpad.output.push(data);
});
if (!weight) {
weight = remaining - getRemainingUnits(); // How many units are consumed by a single iteration
}
scratchpad.index++;
} while (scratchpad.index < count);