Create and Submit a Task with Dependent Scripts

The following sample creates a map/reduce script task. It then creates an asynchronous search task and adds the map/reduce script task to the search task as dependent scripts. The script is processed when the search task is complete. For more information, see SuiteCloud Processors.

This sample refers to two script parameters: custscript_ss_as_srch_res for the scheduled script, and custscript_mr_as_srch_res for the map/reduce script. These parameters are used to pass the location of the CSV file to the dependent scripts, which is shown in the second and third code samples below. Before using this sample, create these parameters in the script record. For more information, see Creating Script Parameters.

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.

          /**
 * @NApiVersion 2.1
 */

require(['N/task'], (task) => {
    // Specify a file for the search results
    const asyncSearchResultFile = 'SuiteScripts/ExportFile.csv';

    // Create a map/reduce script task
    const mapReduceScript = task.create({
        taskType: task.TaskType.MAP_REDUCE
    });
    mapReduceScript.scriptId = 'customscript_mr_create_submit';
    mapReduceScript.deploymentId = 'customdeploy_mr_create_submit';
    mapReduceScript.params = {
        'custscript_mr_as_srch_res' : asyncSearchResultFile
    };

    // Create the search task
    const asyncTask = task.create({
        taskType: task.TaskType.SEARCH
    };
    asyncTask.savedSearchId = 'customsearch35';
    asyncTask.filePath = asyncSearchResultFile;

    // Add dependent scripts to the search task before it is submitted
    asyncTask.addInboundDependency(mapReduceScript);

    // Submit the search task
    const asyncTaskId = asyncTask.submit();
}); 

        

To read the contents of the search results file in a dependent map/reduce script, consider the following script sample:

          /**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 * @NModuleScope SameAccount
 */

define(['N/runtime', 'N/file', 'N/log', 'N/email'], (runtime, file, log, email) => {
    // Load the search results file, count the number of letters in the file, and
    // store this count in another file

    function getInputData() {
        // Retrieve the ID of the search results file
        //
        // Update the completionScriptParameterName value to use the script
        // ID of the original search task
        const fileId = runtime.getCurrentScript().getParameter({
            name: 'custscript_mr_new_submit_task_field'
        });

        if (!fileId) {
            log.error({
                details: 'fileId is not valid. Please check the script parameter stored in the completionScriptParameterName variable in getInputData().'
            });
        }

        return {
            type: 'file',
            id: fileId
        };
    }

    function map(context) {
        log.debug('map context: ', context);
        context.write({
           key: context.key,
           value: 1           
        });
    }

    function reduce(context) {
        log.debug('reduce context: ', context);
        log.debug('context.values.length', context.values.length);
        
        // Count the number of rows 
        let rowCount = 0;
        rowCount = context.values.length - 1 // Subtracting 1 to remove header row from the count.
        log.debug('rowCount: ', rowCount);
 
       // Send an email to the user who ran the script, and attach the
       // CSV file with the search results
       const completionScriptParameterName = 'custscript_mr_new_submit_task_field';
       const resFileId = runtime.getCurrentScript().getParameter({
          name: completionScriptParameterName
       });
       const fileObj = file.load({  
          id: resFileId
       });
       const userId = runtime.getCurrentUser().id;
       email.send({
          author: userId,
          recipients: userId,
          subject: 'Search completed',
          body: 'CSV file attached, ' + rowCount + ' record(s) found.', 
          attachments: [fileObj]            
       });
    }

    function summarize(summary) {
        const type = summary.toString();
        log.audit({ title: type + ' Usage Consumed ', details: summary.usage });
        log.audit({ title: type + ' Concurrency Number ', details: summary.concurrency });
        log.audit({ title: type + ' Number of Yields ', details: summary.yields });
    }
    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce,
        summarize: summarize
    };
}); 

        

General Notices