Does anyone have an example map/reduce script I ca...
# general
b
Does anyone have an example map/reduce script I can use to update line level information in mass, say of JEs?
r
Do you have a specific use case ? Otherwise a search of JE in input stage, followed by loading your JE, updating and saving it in map will work.
b
@raghav I do, but it pertains to updating revenue arrangement line level information. I am in need of a way to update the "Create Revenue Plans On" field in mass.
r
I forgot what an arm (revenue) JE looks like. But I think you should be able to with the above approach. In case for each JE if you need to create multiple other records, before you create the JE then create those records in the map stage and then create and tag those records in JE in the reduce stage.
b
@raghav I am not trying to change the journal entry that is being made, but trying to mass update the revenue elements, as those are the line level information on the arrangements and the only way to update revenue elements
m
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 */
define(['N/record', 'N/search'], function(record, search) {

   
    function getInputData() {
        return search.create({
            type: "revenuearrangement",
            filters: [
                ["type", "anyof", "RevArrng"], 
                "AND", 
                ["internalid","anyof","91518","91519"]
            ],
            columns: [
                search.createColumn({name: "internalid", label: "Internal ID"})
            ]
        });
    }

   
    function map(context) {
        var searchResult = JSON.parse(context.value);
        var recordId = searchResult.id;
        context.write({
            key: recordId,
            value: recordId
        });
    }

  
    function reduce(context) {
        context.values.forEach(function(recordId) {
            try {
                var recordToEdit = record.load({
                    type: 'revenuearrangement', 
                    id: recordId
                });

                var lineCount = recordToEdit.getLineCount({
                    sublistId: 'revenueelement'
                });

                for (var i = 0; i < lineCount; i++) {
                    recordToEdit.setSublistValue({
                        sublistId: 'revenueelement',
                        fieldId: 'createrevenueplanson',
                        line: i,
                        value: '-1' 
                    });
                }

                recordToEdit.save();
            } catch (e) {
                log.error("Error", {
                    "ID": recordId,
                    "Error": e.message
                });
            }
        });
    }

   
    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce
    };
});
b
@Marc The CRPO trigger on many of the items does not match that on the item, so many of these were not creating revenue plans. Basically I need to update CRPO triggers on a bunch of rev elements and the only way I can think to do that in mass is a map/reduce script on the arrangements. I will try your example here in a bit!
1