How to Reschedule the Schedule Script in Netsuite using SuiteScript 2.0 version

耗尽温柔 提交于 2019-12-03 22:30:44

The N/task and N/runtime modules have what you're looking for. You'll use N/task to do the rescheduling, and N/runtime to get the current script info.

Without your exact code I can't give a very specific example, but your scheduled script will end up looking generally something like:

/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 */
define(['N/task', 'N/runtime'], function(task, runtime) {

    /**
     * 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();
    }

    function execute(context) {

        // Do stuff...

        while(...) {
            // Do processing in loop

            // Check remaining usage and reschedule if necessary
            if (runtime.getCurrentScript().getRemainingUsage() < 100) {
                var taskId = rescheduleCurrentScript();
                log.audit("Rescheduling status: " + task.checkStatus(taskId));
                return;
            }
        }
    }

    return {
        execute: execute
    };
});

It seems like scheduledScriptTask.submit() is returning null.

var taskId = rescheduleCurrentScript();

taskId above returns null when logged. Is it possibly a bug in NetSuite side?

The accepted answer is good for occasional yields. In addition to this if you know you are going to often breach the governance limits you may want to consider a Map/Reduce script instead as it has yielding/resuming built in.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!