2016-07-21 91 views

回答

8

N/taskN/runtime模塊有你在找什麼。您將使用N/task執行重新計劃,並使用N/runtime獲取當前腳本信息。

沒有你確切的代碼,我不能舉一個很具體的例子,但你的計劃腳本最終會通常看起來像:

/** 
* @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 
    }; 
}); 
+0

這是否也允許腳本在執行中的同一點處離開?例如,如果我循環播放預定腳本中已保存搜索的結果,那麼此方法是否可以在保存的搜索中以相同結果恢復預定腳本?或者它會從保存的搜索的第一個結果開始? –

+2

無論何時重新安排預定腳本,您都需要在停止的地方繼續進行管理。這通常通過內部ID對結果進行排序,存儲上次處理的ID並過濾搜索大於上次處理的內部ID的搜索。 – erictgrubaugh

+0

哪裏最安全的地方存儲最後處理的ID? –

0

好像scheduledScriptTask.submit()方法返回null。

var taskId = rescheduleCurrentScript(); 

上面的taskId在記錄時返回null。 NetSuite可能是一個bug嗎?