Submitting an On–Demand Map/Reduce Script Deployment from a Script
You can submit map/reduce scripts on demand from another server script type. You can submit a deployment this way by using the task.MapReduceScriptTask. For the call to be successful, the Status field on the script deployment record must be set to Not Scheduled.
You can also submit an on-demand deployment from the UI. For details, see Submitting an On-Demand Map/Reduce Script Deployment from the UI.
Note that after you submit an on-demand deployment of a script, the script does not necessarily execute right away. There might be a short delay before the script runs, even if no other scripts are ahead of it. If other scripts are waiting, this script will wait until they're done. For more details about how map/reduce scripts are processed, see SuiteCloud Processors.
This method will ad-hoc start the Map/Reduce deployment with the identity/role being inherited from the script that called the method. To execute a map/reduce script from a server script, you need the Administrator role or a role with SuiteScript and SuiteScript Scheduling permissions. For more information, see Permissions Documentation.
To submit an on-demand map/reduce script instance from a script:
-
Create your map/reduce script entry point script. This involves uploading a JavaScript file and creating a script record from it. To review a map/reduce script example, see SuiteScript 2.x Map/Reduce Script Type. If this script is your first, see SuiteScript 2.x Hello World and SuiteScript 2.x Entry Point Script Creation and Deployment.
-
Open the appropriate script record in view mode. Click the Deploy Script button.
-
When the script deployment record loads, check the Deployed box, if it's not already checked.
-
Set the Status field to Not Scheduled.
-
Set the remaining body fields. For help understanding the fields, see Map/Reduce Script Deployment Record.
-
Click Save.
-
In the server script where you want to submit the map/reduce script, call task.create(options) to return a task.MapReduceScriptTask object:
var scriptTask = task.create({taskType: task.TaskType.MAP_REDUCE});
-
Set the MapReduceScriptTask.scriptId and MapReduceScriptTask.deploymentId properties:
scriptTask.scriptId = 'customscript1'; scriptTask.deploymentId = 'customdeploy1';
The deployment ID property is optional when creating the map/reduce script task object. If you don't specify a deployment ID, the system will find and use an available deployment record for the script ID. For a deployment to be considered available, three conditions must be met: the deployment record must have a status of Not Scheduled, its Deployed option must be set to true, and no unfinished instances of the deployment can exist.
-
Call MapReduceScriptTask.submit() to submit the script for processing. For example:
scriptTask.submit();
In some cases, you may want to submit a map/reduce script for processing multiple times simultaneously, or within a short time frame. However, the system won't let you submit a script deployment if a previous instance is already submitted and not finished. To solve this, create multiple deployments for the script. In other words, repeat Steps 2 through 9 of the procedure above as needed. If you are using this approach, consider omitting the deploymentId property when you create the map/reduce script task object. When you omit the deploymentId, the system searches for and uses whichever deployment record is available.