Chaining Map/Reduce Scripts in NetSuite

Introduction

When working with large processes in NetSuite, it’s common to split logic across multiple Map/Reduce scripts that need to run in sequence. Since execution times can vary based on data volume and queue availability, using scheduled deployments alone is unreliable.

If one script starts before another finishes, it can create processing issues or duplicate work. A better approach is to chain scripts dynamically using NetSuite’s N/task module.

Why Use Script Chaining?

Instead of scheduling multiple scripts at fixed times, you can:

  1. Run the first Map/Reduce script
  2. Trigger the next script from the summarize() stage
  3. Continue the process as needed

This ensures each script starts only after the previous one has completed.

/**

* @NApiVersion 2.1

*/

 

define([‘N/task’], (task) => {

 

const summarize = () => {

 

const nextTask = task.create({

taskType: task.TaskType.MAP_REDUCE,

scriptId: ‘customscript_next_process’,

deploymentId: ‘customdeploy_next_process’,

params: {

custscript_batch_id: 1001

}

});

 

const taskId = nextTask.submit();

 

log.audit({

title: ‘Next Script Submitted’,

details: taskId

});

};

 

return { summarize };

 

});

 

Example: Submitting the Next Map/Reduce Script

/**

* @NApiVersion 2.1

*/

 

define([‘N/task’], (task) => {

 

const summarize = () => {

 

const nextTask = task.create({

taskType: task.TaskType.MAP_REDUCE,

scriptId: ‘customscript_next_process’,

deploymentId: ‘customdeploy_next_process’,

params: {

custscript_batch_id: 1001

}

});

 

const taskId = nextTask.submit();

 

log.audit({

title: ‘Next Script Submitted’,

details: taskId

});

};

 

return { summarize };

 

});

 

Key task.create() Properties

taskType

Defines the type of task being executed.

task.TaskType.MAP_REDUCE

scriptId

The script ID of the Map/Reduce script to run.

scriptId: ‘customscript_next_process’

deploymentId

The deployment record used for execution.

deploymentId: ‘customdeploy_next_process’

params

Optional parameters passed into the next script.

params: {

custscript_batch_id: 1001

}

Best Practice for 2026

The recommended approach is to submit the next script from the summarize() stage since it only runs after all map and reduce processing has completed.

It’s also a good idea to:

  • Add error handling before submitting the next task
  • Use SuiteScript 2.1 for modern JavaScript support
  • Explicitly define deployments in production environments

Simplified Example

If the script only has one deployment, you can omit the deployment ID:

const myTask = task.create({

taskType: task.TaskType.MAP_REDUCE

});

 

myTask.scriptId = ‘customscript_my_mr_script’;

 

const taskId = myTask.submit();

NetSuite will automatically use the available deployment.

Conclusion

Using the N/task module to chain Map/Reduce scripts provides a much more reliable and scalable solution than relying on multiple scheduled deployments. By triggering the next script from the summarize() stage, you can ensure processes run in the correct order without timing conflicts or unnecessary delays.

As NetSuite environments continue to grow in complexity, this approach has become a standard best practice for building efficient and maintainable SuiteScript automation workflows.