Can we delay schedule script to run only after Map Reduce script?
Hi,
Many times we come across requirement where script should only run after other script finished. However we can not control user event scripts but we can some how control scheduled script and map reduce script.
If you are newbie , you can read about different type of scripts under tutorial , below are detail about other type of scripts which we will cover in this post:
Now coming back to our topic for this post, we want to delay scheduled script and only want to run/process it after completion of Map Reduce script.
Obviously Netsuite does not support any functionality like this but we can use conditions inside scripts to achieve what we need.
How to run Scheduled Script after Map Reduce Script?
Lets say, you are writing user event and you want to trigger scheduled script from user event. Now, there is already one Map Reduce script written by other developer in Account, this Map reduce script process and update fields which you need in your scheduled script. If you trigger Scheduled Script from User Event , it will run and finish processing without knowing if those fields were already updated or not.
To overcome such kind of scenarios, here what we should do:
Step1: Trigger Map Reduce Script inside User Event Script
Step 2: Store Task id in variable
Step 3: Pass stored variable into Scheduled Script through Script Parameters
Step 4: Inside Schedule script, check status of Map Reduce Script. If it is in stage null or SUMMARIZE, proceed , else reschedule Scheduled Script with task id in script parameter.
This way script might reschedule it self for some times but it will only run after Map Reduce Script.
We are adding code below, if it help you share our post with friends also.
Code:
User Event Script:
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/search','N/record', 'N/task', 'N/format'],
function(search, record, task, format) {
function triggerScheduleScript(context) {
// add your logic here
// don't forget to use try catch ever
var mrTask = task.create({taskType: task.TaskType.MAP_REDUCE});
mrTask.scriptId = 'customscript_script_map_reduce';
mrTask.deploymentId = 'customdeploy_id';
var mrTaskId = mrTask.submit();
var scheduledScript = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT
});
scheduledScript.scriptId = 'customscript_ss_id';
scheduledScript.deploymentId = 'customdeploy_ss_id';
scheduledScript.params = {
'custscript_taskid' : mrTaskId
};
var taskid = scheduledScript.submit();
}
return {
afterSubmit: triggerScheduleScript
};
});
Scheduled Script:
/**
*@NApiVersion 2.x
*@NScriptType ScheduledScript
*/
define(['N/search', 'N/record', 'N/email', 'N/runtime', 'N/render', 'N/format', 'N/task'],
function(search, record, email, runtime, render, format, task) {
function execute(context) {
var mrTaskId = runtime.getCurrentScript().getParameter("custscript_taskid") || '';
log.audit({
title: 'mrTaskId',
details: mrTaskId
});
if(mrTaskId )
{
var summary = task.checkStatus(mrTaskId);
if ((summary.stage === task.MapReduceStage.SUMMARIZE) || (summary.stage === null))
{
//almost done, allow script to go further
}
else
{
//processing MAP REDUCE Script, reschedule same processing
var scheduledScript = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT
});
scheduledScript.scriptId = 'customscript_ss_id';
scheduledScript.deploymentId = 'customdeploy_ss_id';
scheduledScript.params = {
'custscript_taskid' : mrTaskId
};
var taskid = scheduledScript.submit();
return false;
}
}
}
return {
execute: execute
};
});
Hope this will help, let us know if you find any issue.
*** Please note, you can also wait for other schedule script to finish. You need task id and reschedule your Scheduled script.
Download Netsuite Guru Android App
Follow Me on Linkedin
Our FB Page
Comments
Post a Comment
Thanks for you message, please join us on Facebook and Linkedin