user15989047
user15989047

Reputation:

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

I am trying to only load and save a work order record using the map-reduce script. But I don't see logs for loaded work orders or saved work orders. the script is executing only until work_order_Id. 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);
      

    }
    
   

I am trying to load and save work order record. But its not executing.I am trying to load and save a work order record. but it's not loading.

Upvotes: 0

Views: 426

Answers (1)

Steffen Andersland
Steffen Andersland

Reputation: 609

I usually like to simply return saved searches in getInputData because it's consistent to work with and you don't have to fuss with the going over 4k search results and having to return arrays of objects that you put together yourself. Usually transforming data to be in the format you want is best done in the map stage.

/**
 * @NScriptType MapReduceScript
 * @NApiVersion 2.x
 */
define(["N/search", "N/record"], function (search, record) {
function getInputData() {
    // run a saved search of work orders
    return 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"}),
        ]
    });    
}

function map(context) {
    
    // get the work order id
    var workOrderId = JSON.parse(context.value).id;    
    log.debug("workOrderId", workOrderId);
    
    // load the work order
    var wordOrderRecord = record.load({
        type: record.Type.WORK_ORDER,
        id: work_order_Id,
        isDynamic: true,
    });
    log.debug("wordOrderRecord", wordOrderRecord);
    
    // save the work order
    var recId = wordOrderRecord.save({
        enableSourcing: true,
        ignoreMandatoryFields: true
    });
    log.debug("recId",recId);
}

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,
    summarize: summarize,
};
})

Upvotes: 0

Related Questions