Casebook04 · SuiteScript
Yield and reschedule tricks
Long scheduled scripts yield or reschedule themselves with a saved position. When that bookkeeping fails, records are skipped or processed twice.
- Casebook 04
- SuiteScript
What you notice
A nightly job usually finishes, but some nights it leaves records unprocessed, or processes a batch twice. The script log is full of “rescheduling” messages.
Why it happens
1.0 scheduled scripts had to manage their own limits. The common pattern checks getRemainingUsage(), then calls nlapiYieldScript() or reschedules itself with the last processed ID saved somewhere. Every script implements that slightly differently, and a crash between “processed” and “saved my position” breaks it.
The fix in 2.1
Use a Map/Reduce script. getInputData returns the search, map handles one record, and NetSuite takes care of batching, governance and restarts. Errors for individual records are collected in summarize instead of stopping the job.
const getInputData = () => search.create({ type: 'customer', filters: [['custentity_needs_update', 'is', 'T']] });
const map = (context) => {
const { id } = JSON.parse(context.value);
record.submitFields({ type: 'customer', id, values: { custentity_needs_update: false } });
};
const summarize = (summary) => summary.mapSummary.errors.iterator().each((key, err) => {
log.error({ title: `Customer ${key} failed`, details: err }); return true;
});
Short, quick jobs can stay as scheduled scripts. The move pays off for anything that used to yield.
What to check in your account
Search for nlapiYieldScript, nlapiScheduleScript called from inside the same script, and checks on getRemainingUsage.