Map Reduce script : Update custom field with a field value

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */

define(['N/file', 'N/search','N/record'], function (file, search, record) {

//Map reduce script for storing the value of  customer name to the custom field "name_field".
    const savedSearch = {
        // The saved search for finding all the active customer records
        customerSearch() {
            var customerSearchObj = search.create({
                type: "customer",
                filters:
                    [
                        ["stage", "anyof", "CUSTOMER"],
                        "AND",
                        ["isinactive", "is", "F"]
                    ],
                columns:
                    [
                        search.createColumn({
                            name: "entityid",
                            sort: search.Sort.ASC,
                            label: "ID"
                        }),
                        search.createColumn({ name: "altname", label: "Name" })
                    ]
            });
            var searchResultCount = customerSearchObj.runPaged().count;
            log.debug("customerSearchObj result count", searchResultCount);//prints the number of search results
            var cus = []
            customerSearchObj.run().each(function (result) {
                // .run().each has a limit of 4,000 results
                
		var tempobj = {}

                tempobj.internalId=result.id
                tempobj.name = result.getValue({
                    name: "altname",
                    label: "Name"
                })
                cus.push(tempobj)
                return true;

            });
            return cus;
        }
    }

    function getInputData() {

        var customerResultArray = savedSearch.customerSearch();
        return customerResultArray;
    }


    function reduce(reduceContext) {
        var dataObj = JSON.parse(reduceContext.values[0]);
       
        //Seting the customer name into the custom field "name_field" of record type customer

         record.submitFields({
            type: record.Type.CUSTOMER,
            id: dataObj.internalId,
            values: {
                custentity_name_filed: dataObj.name
            },
            options: {
                enableSourcing: false,
                ignoreMandatoryFields : true
            }
        });



    }

    function summarize() {

    }

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

Leave a comment

Your email address will not be published. Required fields are marked *