Create and Submit a Map/Reduce Script Task

The following sample submits a map/reduce script task for processing. Before you use this sample, you must create a map/reduce script file, upload the file to your NetSuite account, and create a script record and script deployment record for it. For help working with map/reduce scripts, see SuiteScript 2.x Map/Reduce Script Type. You must also edit the sample and replace all hard-coded IDs with values that are valid in your NetSuite account.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

          /**
 * @NApiVersion 2.1
 */
require(['N/task', 'N/runtime', 'N/email'], (task, runtime, email) => {
    // Store the script ID of the script to submit
    //
    // Update the following statement so it uses the script ID
    // of the map/reduce script record you want to submit
    const mapReduceScriptId = 'customscript_test_mapreduce_script';
 
    // Create a map/reduce task
    //
    // Update the deploymentId parameter to use the script ID of
    // the deployment record for your map/reduce script
    let mrTask = task.create({
        taskType: task.TaskType.MAP_REDUCE,
        scriptId: mapReduceScriptId,
        deploymentId: 'customdeploy_test_mapreduce_script'
    });
 
    // Submit the map/reduce task
    let mrTaskId = mrTask.submit();
 
    // Check the status of the task, and send an email if the
    // task has a status of FAILED
    //
    // Update the authorId value with the internal ID of the user
    // who is the email sender. Update the recipientEmail value
    // with the email address of the recipient.
    let taskStatus = task.checkStatus(mrTaskId);
    if (taskStatus.status === 'FAILED') {
        const authorId = -5;
        const recipientEmail = 'notify@myCompany.com';
        email.send({
            author: authorId,
            recipients: recipientEmail,
            subject: 'Failure executing map/reduce job!',
            body: 'Map reduce task: ' + mapReduceScriptId + ' has failed.'
        });
    }
}); 

        

General Notices