Rescheduling Tasks using scheduling script

Scenario: Example for rescheduling tasks using scheduling task

For rescheduling, you need to import ‘N/task’ module first. It is needed for creating each rescheduling tasks after reaching the limit of remaining governance.

function rescheduleScriptandReturn(endRange,nextRange) {
            try {
                //endrange is the last range of the just finished search.
                //Next search range when rescheduled will start from this endRange
                log.debug("RESCHEDULING STARTS")
                // log.debug("ID: ",id)
                var ssTask = task.create({
                    taskType: task.TaskType.SCHEDULED_SCRIPT,
                    scriptId: 2109,
                    deploymentId: "customdeploy_jj_ss_scheduled_script_fa",
                    params: {
                        custscript_rec_id: id
                        custscript_endrange: endRange
                        custscript_tot_count: n
                    }
                });
                var scriptTaskId = ssTask.submit();
                log.debug("RESCHEDULED SCRIPT TASK ID: ", scriptTaskId)

            } catch (err) {
                log.debug("Error @ rescheduling: ",err.name+": "+err.message)
                log.error({
                    title: 'error on rescheduleScriptandReturn',
                    details: err
                });
            }

        }

function call_schedule(){
  try{
     var scriptObj = runtime.getCurrentScript();// getting the details of currently executing script
     var remaingUsage = scriptObj.getRemainingUsage()// returns remaining governance of the script
     if(remaingUsage<1000){
        rescheduleScriptandReturn(0,50);
     }
  }
  catch(e){
    log.debug("Error: ",e.name+" : "+e.messsage)
   }
}

You can just call this function whenever you want rescheduling. You can change the parameters of the function according the requirement. In this example, I just put a starting and ending range of the records to reschedule. ie; start range will store the index of the record which you want to start scheduling and end range stores the total number of records you want to schedule in each scheduling. Eg: if rescheduleScriptandReturn(0,50), it means, it will start scheduling from the index 0 of the record and from 0, it will schedule the next 50 records. ie, index 0 to 50.

NB: If you want to get remaining governance of your script, you need to use runtime.getCurrentScript(), and if you want to use this, you need to import the module ‘N/currentScript’

Leave a comment

Your email address will not be published. Required fields are marked *