/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/search', 'N/runtime', 'N/task', 'N/file'],
function (record, search, runtime, task, file) {
/**
* Reschedules the current script and returns the ID of the reschedule task
*/
function rescheduleCurrentScript() {
var scheduledScriptTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT
});
scheduledScriptTask.scriptId = runtime.getCurrentScript().id;
scheduledScriptTask.deploymentId = runtime.getCurrentScript().deploymentId;
return scheduledScriptTask.submit();
}
/**
* Definition of the Scheduled script trigger point.
* @param {Object} context
* @param {string} context.type - The context in which the script is executed. It is one of the values from the context.InvocationType enum.
* @Since 2015.2
*/
function execute(context) {
// Add a search to run the scheduled process based on a search result
var customSearchObj = search.create({
///////Add search code
});
customSearchObj.run().each(function (res) {
// Check remaining usage and reschedule if necessary
if (runtime.getCurrentScript().getRemainingUsage() < 100) {
// Reschedule script if remaining usage is less than 100
var taskId = rescheduleCurrentScript();
log.debug("Rescheduling status: " + task.checkStatus(taskId));
return false;
}
try {
// ADD THE SCHEDULED SCRIPT CODE TO RUN THE REQUIRED FUNCTIONALITY
} catch (e) {
// error handling
}
return true;
})
}
return {
execute: execute
}
}
);
Script to reschedule a scheduled script within the script based on usage to avoid ‘SSS_USAGE_LIMIT_EXCEEDED_ERROR’ in NetSuite scheduled script.