Stop scheduled script using parameter
Hi all,
i wrote a scheduled script for un drop off orders , i need to control the script by check box so when it is true the script run and when it is disable the script stop, i create a user script parameter with type check box, but i can’t control the script after i execute it
any help will be appreciated
/**
* @NApiVersion 2.0
* @NScriptType ScheduledScript
*/
define([‘N/search’, ‘N/record’, ‘N/runtime’, ‘N/task’, ‘N/log’], function(search, record, runtime, task, log) {
function execute(context) {
if (context.type == context.InvocationType.ON_DEMAND)
return;
var searchStop = runtime.getCurrentScript().getParameter(‘custscript2’);
log.debug({ title: ‘STOPONOFF’, details: searchStop });
var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
var scriptID = runtime.getCurrentScript().id;
var mySalesOrderSearch = search.load({
id: ‘customsearch1199’
});
function stop() {
var scheduledScript = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT
});
scheduledScript.scriptId = scriptID;
scheduledScript.deploymentId = ‘customdeploy9’;
scheduledScript.params = {
searchStop: false
};
scheduledScript.submit();
}
var resultSet = mySalesOrderSearch.run();
var results = resultSet.getRange({ start: 0, end: 1000 });
for (var i = 0; i < results.length; i++) {
var myrec = record.load({
type: record.Type.SALES_ORDER,
id: results[i].id,
isDynamic: true
})
undropoff(myrec);
remainingUsage = runtime.getCurrentScript().getRemainingUsage();
searchStop = runtime.getCurrentScript().getParameter({ name: ‘custscript2’ });
log.debug({ title: ‘remainingUsage’, details: remainingUsage });
if (remainingUsage <= 100 || i == 999) {
log.debug({ title: ‘STOPONOFF’, details: searchStop });
var scrStatus = task.checkStatus(scheduledScript);
if (scrStatus.status == ‘QUEUED’)
stop();
break;
}
// log.debug({ title: ‘sales order id’, details: scrStatus.status });
}
function undropoff(dyRec) {
log.debug({ title: ‘sales order id’, details: dyRec.id });
dyRec.setValue({
fieldId: ‘custbody_sor_dropoffstatus’,
value: ‘NOT_STARTED’
}).setValue({
fieldId: ‘custbody_sor_dropoffqr’,
value: ”
}).setValue({
fieldId: ‘custbody_sor_dropoffimageurl’,
value: ”
}).setValue({
fieldId: ‘custbody_sor_dropoffstarttime’,
value: ”
}).setValue({
fieldId: ‘custbody_sor_dropoffarrivetime’,
value: ”
}).setValue({
fieldId: ‘custbody_sor_dropoffcompletetime’,
value: ”
}).setValue({
fieldId: ‘custbody_sor_dropoffcanceltime’,
value: ”
}).setValue({
fieldId: ‘custbody_sor_dropoffcancelreason’,
value: ”
}).setValue({
fieldId: ‘custbody_ord_dropoffcashtocollect’,
value: ‘0’
}).save({
enableSourcing: true,
ignoreMandatoryFields: false
});
log.debug({
title: ‘Debug’,
details: ‘Done’ + dyRec.id
});
}
}return {
execute: execute
};
});
You will need to explain more. Your script does not appear to use the value of searchStop
for anything other than logging. I would expect your script to use the return
keyword to stop early. I personally would expect the return to be in the loop, or perhaps at the beginning of the script if it had logic to reschedule itself.
You appear to use some weird rescheduling task to schedule a task that just reschedules itself and returns. You also appear to be making a scoping mistake by using the scheduledScript variable out of scope and probably incorrectly since task.checkStatus
is designed to work with the taskId you get from submitting the task, not the task itself.
That said, I believe your problem will be that using runtime.getCurrentScript().getParameter
will not get you updated values for your checkbox, It will only get you the value of the parameter when the script starts. It will still have the original value even if it changes in the middle of script execution. Expect to use N/record to load the deployment record, where the script parameter will appear on the script deployment record as a custom field.
Hi @battk ,thanks for answer , sorry i’m net to ss2.0 it is my 3rd script ,my original scheduled script is this, i tried to control it using parameter by load the deployment record then get the value of parameter but it seems like i cant modify the value of the parameter after run, should i use another method
// get parameter value
You need to keep in mind I have no idea what you are doing, and especially not why you are doing it. Usually a checkbox on the script parameter to stop a script is a manual intervention sort of thing.
Checkboxes in suitescript 2 are usually booleans: true or false (occasionally a falsy value like undefined or ”). Usually you wouldnt compare a checkbox field’s value to the string T
You can’t use script parameters to control a scheduled script like you’re trying to. When a scheduled script is executed, NetSuite takes a snap shot of the Script parameter values and that’s all the script can access during that invocation.
runtime.getCurrentScript().getParameter()
Is looking up values from this snapshot. When it’s started over it will pick up any changes to params. For what you want to do you’re better off with a custom record handling your runtime control, then the script fetches that single record at whatever frequency appropriate. Mind you, this is a bad practice to be hammering the database like that, but not too bad as custom record access is fast, especially considering you can use search.lookupFields
I was thinking if there was a way to use the session state repo for this, but the scheduled job will run as System and the SuiteLet (What I was thinking could serve as a control panel) will run as a user. Maybe a SuiteLet that ran a scheduled job to set the session value, but Yuck! – that would be pretty ugly.
Steveklett
Can you edit your post and use the code formatting? It makes it so much easier to read