1
votes

I have a scheduled script that does two things. I have checkbox parameters to determine if those things are require on this run of the script. The first time around both are defaulted to true.

function execute(scriptContext) {
    var script = runtime.getCurrentScript();
    doTask1 = script.getParameter({name: "custscript_tmh_do_task1"});
    doTask2 = script.getParameter({name: "custscript_tmh_do_task2"});

    if(doTask1){
        //Do something
    }
    if(doTask2){
        //Do something
    }

    //Determine if task1 or task2 needs to happen and set them to true or false.

    //Reschedule script if required
    if(doTask1 || doTask2){
    var scheduledScriptTask = task.create({
        taskType: task.TaskType.SCHEDULED_SCRIPT
    });
    scheduledScriptTask.scriptId = runtime.getCurrentScript().id;
    scheduledScriptTask.deploymentId = runtime.getCurrentScript().deploymentId;
    scheduledScriptTask.params = {'custscript_tmh_do_task1': doTask1 ,
                                    'custscript_tmh_do_task2': doTask2 };
    return scheduledScriptTask.submit();
    }
}

The problem is the second time around, it is not doing the tasks when the booleans are set to true. It just skips over them. I have used the debugger to confirm at the point of rescheduling the script the booleans are true.

Question: How do I reschedule a scheduled script with setting a boolean value.

Side Question" I have the parameters created as a script parameter. Is it possible to do this without setting the script parameters in NetSuite?

1
I recommend logging out the parameter values to confirm they actually hold the values you expect. There are also certain situations where checkboxes still need to use the old 1.0 "T" or "F" values instead of true and false, so perhaps try setting them that way in your rescheduling logic. - erictgrubaugh

1 Answers

2
votes

that's not the correct constructor for generating the task. I always use:

var scheduledScriptTask = task.create({
    taskType: task.TaskType.SCHEDULED_SCRIPT,
    scriptId: runtime.getCurrentScript().id,
    deploymentId: runtime.getCurrentScript().deploymentId,
    params: {'custscript_tmh_do_task1': doTask1 ,
             'custscript_tmh_do_task2': doTask2 }
});

Also script parameters are passed as strings so you need to convert.

either:

doTask1 = 'T' == script.getParameter({name: "custscript_tmh_do_task1"});

or

doTask1 = script.getParameter({name: "custscript_tmh_do_task1"});
...
if('T' == doTask1){ ...

Checking when you use them ('T' == doTask1) lets you just round trip the string values into your next iteration. If you use them as booleans so you can decide whether the next iteration needs to do both tasks then you'll need to turn them back to strings when you pass them to the next iteration: custscript_tmh_do_task1 : doTask1 ? 'T' : 'F'

You do not need to give the parameters default values in order to pass them on to the next iteration. And any values you give in the task.create method will override the configured values.