RE: 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);
      

    }
    
   
Maira S Beginner Asked on February 16, 2023 in SuiteScript.
Add Comment
12 Answers

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.