How to load and save work order record using map reduce script?

I am trying to load and save work order record using  map reduce script. But after executing, I am not getting logs for load object and save object.Please Help!

Below is my code:

 function getInputData(){
    var process_data =[];
        try{
            var workorderSearchObj = search.create({
               type: "workorder",
               filters:
               [
                  ["type","anyof","WorkOrd"], 
                  "AND", 
                  ["mainline","is","T"], 
                  "AND", 
                  ["status","anyof","WorkOrd:A","WorkOrd:B","WorkOrd:D"]
               ],
               columns:
               [
                  search.createColumn({name: "internalid", label: "Internal ID"}),
                  search.createColumn({name: "tranid", label: "Document Number"})
               ]
            });
            var searchResultCount = workorderSearchObj.runPaged().count;
            log.debug("workorderSearchObj result count",searchResultCount);
            workorderSearchObj.run().each(function(result){
               // .run().each has a limit of 4,000 results
               var work_Order = result.getValue({name:'internalid'});
               var document_no = result.getValue({name:'tranid'});
               process_data.push({
                'work_Order':work_Order,
                'document_no':document_no
            });
               return true;
            });
            
        
        }catch(error){
            log.debug(error);
        }   
            return process_data;
    }
     function map(context){
        var process_data=JSON.parse(context.value);
        log.debug('process_data',process_data);
        var work_order_Id = process_data.work_Order;
        log.debug("work_order_Id",work_order_Id);
        var work_Order_obj = record.load({
            type: record.Type.WORK_ORDER,
            id: work_order_Id,
            isDynamic: true
        }); 
       log.debug("work_Order_obj",work_Order_obj);
        var recId=work_Order_obj.save({
            enableSourcing: true,
            ignoreMandatoryFields: true
        });
       log.debug("recId",recId);
      

    }
    
   
Beginner Asked on February 16, 2023 in SuiteScript.
Add Comment
12 Answer(s)

Hi,

Do you have a a summarize section to log any errors? If not, please add something like:

function summarize(summaryContext) {
    // Handle INPUT errors.
    let input_error_string = '';
    if (summaryContext.inputSummary.error) {
        input_error_string += `GET INPUT DATA ERRORS\n\n`;
        input_error_string += `[Details: ${summaryContext.inputSummary.error}]\n\n`;
    }

    // Handle MAP errors.
    let map_error_string = '';
    summaryContext.mapSummary.errors.iterator().each(function (key, err) {
        if (map_error_string.length === 0) {
            map_error_string = `MAP ERRORS\n\n`;
        }
        map_error_string += `[MAP    Error for Key: ${key} Details: ${err}]\n\n`;
        return true;
    });

    // Handle REDUCE errors.
    let reduce_error_string = '';
    summaryContext.reduceSummary.errors.iterator().each(function (key, err) {
        if (reduce_error_string.length === 0) {
            reduce_error_string = `REDUCE ERRORS\n\n`;
        }
        reduce_error_string += `[REDUCE Error for Key: ${key} Details: ${err}]\n\n`;
        return true;
    });

    if (input_error_string.length > 0 || map_error_string.length > 0 || reduce_error_string.length > 0) {
        log.error({
            title: 'ERROR',
            details: `${input_error_string}${map_error_string}${reduce_error_string}`
        });

        throw error.create({
            name: 'MAP_REDUCE_ERROR',
            message: `${input_error_string}${map_error_string}${reduce_error_string}`
        });
    }

    log.audit('INFO', 'END of script execution.');
};

Thanks,

Chris

Intermediate Answered on February 16, 2023.
Add Comment

Hi Chris, Thanks for response!

I am getting error while uploading  this script

error:Syntax error: missing ; before statement

Beginner Answered on February 16, 2023.
Add Comment

Please change to SuiteScript 2.1:

* @NApiVersion 2.1

Or change all “let” to “var”.

Intermediate Answered on February 16, 2023.
Add Comment

After executing script getting these errors:

org.mozilla.javascript.EcmaError: ReferenceError: “error” is not defined. (/SuiteScripts/MR_work_order.js#130)

${input_error_string}${map_error_string}${reduce_error_string}

Beginner Answered on February 16, 2023.
Add Comment

Please make sure you have the error module loaded:

define([..., 'N/error', ...], function(..., error, ...)

Thanks,

Chris

 

Intermediate Answered on February 16, 2023.
Add Comment

Hi Chris,

Using Summarize stage I got following error:

Error TypeError 2/16/2023 6:22 am -System- Cannot call method “load” of undefined

Beginner Answered on February 16, 2023.
Add Comment

In that case, you have not loaded the record module:

define([..., 'N/record', ...], function(..., record, ...)
Intermediate Answered on February 16, 2023.
Add Comment

I have already added the record module

define([“N/search”, “N/record”],
Beginner Answered on February 16, 2023.
Add Comment

Could you please share your entire script as it is now.

Intermediate Answered on February 16, 2023.
Add Comment

Yes Sure!

/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 * @NModuleScope SameAccount
 */
 define([“N/search”, “N/record”],
    function(search,plugin,record,runtime,format,config,task){
     /**
     * Marks the beginning of the Map/Reduce process and generates input data.
     *
     * @typedef {Object} ObjectRef
     * @property {number} id – Internal ID of the record instance
     * @property {string} type – Record type id
     *
     * @return {Array|Object|Search|RecordRef} inputSummary
     * @since 2015.1
     */
        function getInputData(){
        var process_data =[];
            try{
                var workorderSearchObj = search.create({
                   type: “workorder”,
                   filters:
                   [
                      [“type”,”anyof”,”WorkOrd”],
                      “AND”,
                      [“mainline”,”is”,”T”],
                      “AND”,
                      [“status”,”anyof”,”WorkOrd:A”,”WorkOrd:B”,”WorkOrd:D”]
                   ],
                   columns:
                   [
                      search.createColumn({name: “internalid”, label: “Internal ID”}),
                      search.createColumn({name: “tranid”, label: “Document Number”})
                   ]
                });
                var searchResultCount = workorderSearchObj.runPaged().count;
                log.debug(“workorderSearchObj result count”,searchResultCount);
                workorderSearchObj.run().each(function(result){
                   // .run().each has a limit of 4,000 results
                   var work_Order = result.getValue({name:’internalid’});
                   var document_no = result.getValue({name:’tranid’});
                   process_data.push({
                    ‘work_Order’:work_Order,
                    ‘document_no’:document_no
                });
                   return true;
                });
            }catch(error){
                log.debug(error);
            }
                return process_data;
        }
         function map(context){
            var process_data=JSON.parse(context.value);
            //log.debug(‘process_data’,process_data);
            var work_order_Id = process_data.work_Order;
            //var amount = process_data.document_no;
            log.debug(“record”,record);
            var work_Order_obj = record.load({
                type: record.Type.WORK_ORDER,
                id: work_order_Id,
                isDynamic: true
            });
            log.debug(“work_Order_obj”,work_Order_obj);
            var recId=work_Order_obj.save({
                enableSourcing: true,
                ignoreMandatoryFields: true
            });
            log.debug(“recId”,recId);
        }
         /**
     * Executes when the reduce entry point is triggered and applies to each group.
     *
     * @param {ReduceSummary} context – Data collection containing the groups to process through the reduce stage
     * @since 2015.1
     */
    function reduce(context) {
    }
    /**
     * Executes when the summarize entry point is triggered and applies to the result set.
     *
     * @param {Summary} summary – Holds statistics regarding the execution of a map/reduce script
     * @since 2015.1
     */
        function summarize(context) {
    // log any errors that might occur
    context.mapSummary.errors.iterator().each(function (_, errJson) {
        var error = JSON.parse(errJson);
        log.error({title: error.name, details: error.message});
        return true;
    });
}
    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce,
        summarize: summarize
    };
});
Beginner Answered on February 16, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Become a Sponsor   Become a Sponsor