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

南笙酒味 提交于 2019-12-05 07:28:25

问题


I want to reschedule the schedule script , when the schedule script usage before hitting the governor limits. schedule script in Netsuite has 10,000 units. In SuiteScript 1.0 version, rescheduling is acheived by "nlapiScheduleScript() api " but in SuiteScript 2.0 version how to reschedule the script.

help me to achieve this,thanks in advance.


回答1:


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



回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/38495014/how-to-reschedule-the-schedule-script-in-netsuite-using-suitescript-2-0-version

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