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

 

butterflyeffect Rookie Asked on February 14, 2021 in SuiteScript.

Can you edit your post and use the code formatting? It makes it so much easier to read

on February 16, 2021.
Add Comment
2 Answers

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.

Rookie Answered on February 16, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.