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

 

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 Answer(s)

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.

Advanced Answered on February 14, 2021.

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

// load deployment record
            var objRecord = record.load({
                type: record.Type.SCRIPT_DEPLOYMENT,
                id: depid.id,
                isDynamic: true
            });

// get parameter value

  var parvalue=objRecord.getParameter(‘custscript2’);
           if (parvalue==’T’)
           return;
// original code without stop
/**
 * @NApiVersion 2.0
 * @NScriptType ScheduledScript
 */
define([‘N/search’, ‘N/record’, ‘N/runtime’, ‘N/task’, ‘N/log’], function(search, record, runtime, task, log) {
    function execute() {
        var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
        log.debug({ title: ‘remainingUsage’, details: remainingUsage });
        var scriptID = runtime.getCurrentScript().id;
        var mySalesOrderSearch = search.load({
            id: ‘customsearch1199’
        });
        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();
            log.debug({ title: ‘remainingUsage’, details: remainingUsage });
            if (remainingUsage <= 100 || i == 999) {
                var scrStatus = task.checkStatus(scriptID);
                if (scrStatus.status == ‘QUEUED’)
                    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
    };
})
on February 15, 2021.

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

 

on February 15, 2021.
Add Comment

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.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor   Become a Sponsor