Update sales order in NetSuite where order status is pending fulfillment through map/reduce

2.1k Views Asked by At

I need to do following operation performed when my Map/Reduce suiteScript run. It search all sales order where status is Pending Fulfillment, and update their custom checkbox field (field key is my_custom_field_is_proccessed).

How I am trying to do is like below;

/**
         * @NApiVersion 2.0
         * @NScriptType MapReduceScript
         */
define(['N/search', 'N/record'], function (search, record) {

    function getInputData() {
        var filter1 = search.createFilter({
            name: 'orderstatus',
            operator: search.Operator.IS,
            values: 'Pending Fulfillment'
        });
        return search.create({
            type: search.Type.SALES_ORDER,
            columns: [],
            filters: [filter1]
        });
    }

    function map(context) {

        try {
            var data = JSON.parse(context.value); //read the data
            var transId = data.tranid;
            var orderstatus = data.orderstatus
            var isProcessed = data.my_custom_field_is_proccessed

            /*
              var order = record.save({
             'fromId':data.values['internalid'].value,
             'my_custom_field_is_proccessed':true
              });
            */

            // Update as per below answer
            data.forEach(function (order) {
                var id = record.submitFields({
                    type: 'salesOrder',
                    id: order.internalid,
                    values: {
                        my_custom_field_is_proccessed: true
                    },
                    options: {
                        enableSourcing: false,
                        ignoreMandatoryFields: true
                    }
                });
            });


        } catch (ex) {
            log.error({ title: 'map: error saving records', details: ex });
        }

    }

    function reduce(context) {
        // your code here ...
    }

    function summarize(summary) {
        // your code here ...
    }

    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce,
        summarize: summarize
    };
});  
2

There are 2 best solutions below

1
On

Use record.submitFields instead of record.save.

var id = record.submitFields({
    type: recordType,
    id: id,
    values: {
        fieldId: value
    },
    options: {
        enableSourcing: false,
        ignoreMandatoryFields : true
    }
});
1
On

Can you give try with this code with few changes

         /**
         * @NApiVersion 2.0
         * @NScriptType MapReduceScript
         */
define(['N/search', 'N/record'], function (search, record) {

    function getInputData() {
var arrResults = [];
        var filter1 = search.createFilter({
            name: 'orderstatus',
            operator: search.Operator.IS,
            values: 'Pending Fulfillment'
        });
        return search.create({
            type: search.Type.SALES_ORDER,
            columns: ["internalid"],
            filters: [filter1]
        });
             var id = result.getValue({name: "internalid"});
                log.debug({ title: 'id',details: id});
                if(id) {
                    arrResults.push({"id": id, "custom_field_id":custom_field_id});
                }
return arrResults;
    }

    function map(context) {

        try {
            var data = JSON.parse(context.value); //read the data
            var recordId = data.id;
            var transId = data.tranid;
            var orderstatus = data.orderstatus
            var isProcessed = data.my_custom_field_is_proccessed
    var recordUpdateData = {
                    "type": record.Type.SALES_ORDER,
                    "id": recordId,
                    "values": {}
                };
           recordUpdateData["values"][custom_field_id] =  10;

        record.submitFields(recordUpdateData);

        } catch (ex) {
            log.error({ title: 'map: error saving records', details: ex });
        }

    }

    function reduce(context) {
        // your code here ...
    }

    function summarize(summary) {
        // your code here ...
    }

    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce,
        summarize: summarize
    };
});