Submitting an On–Demand Map/Reduce Script Deployment from a Script

Map/reduce scripts can be submitted for processing on an on-demand basis 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. After a script is submitted for processing, there may be a short system delay before the script is executed, even if no scripts are before it. If there are scripts already waiting to be executed, the script may wait to be executed until other scripts have completed. 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. Executing a map/reduce script from a server script requires either the Administrator role or a role with the SuiteScript and SuiteScript Scheduling permissions. For more information, see Permissions Documentation.

To submit an on-demand map/reduce script instance from a script:

  1. Create your map/reduce script entry point script. This process includes uploading a JavaScript file and creating a script record based on that file. 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.

  2. Open the appropriate script record in view mode. Click the Deploy Script button.

  3. When the script deployment record loads, check the Deployed box, if it is not already checked.

  4. Set the Status field to Not Scheduled.

  5. Set the remaining body fields. For help understanding the fields, see Map/Reduce Script Deployment Record.

  6. Click Save.

  7. 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}); 
    
                  
  8. Set the MapReduceScriptTask.scriptId and MapReduceScriptTask.deploymentId properties:

                    scriptTask.scriptId = 'customscript1';
    scriptTask.deploymentId = 'customdeploy1'; 
    
                  

    Note that the deployment ID property is optional when creating the map/reduce script task object. If the deployment ID property is omitted, the system will search for and use any deployment record that is available to the corresponding 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.

  9. Call MapReduceScriptTask.submit() to submit the script for processing. For example:

                    scriptTask.submit(); 
    
                  
Note:

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 does not permit a script deployment to be submitted if a previous instance of the deployment has already been submitted and is not yet finished. The solution in this case is to 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.

Related Topics

Map/Reduce Script Submission
Map/Reduce Script Deployment Record
Scheduling a Map/Reduce Script Submission
Submitting an On-Demand Map/Reduce Script Deployment from the UI
Submitting Multiple Deployments of the Same Script

General Notices