I have a map/reduce script that is supposed to upd...
# suitescript
l
I have a map/reduce script that is supposed to update the location field of certain advanced intercompany JE lines based on a saved search. Based on the logs, it does not loop through the JE lines. It only loops through the line 0, and stops there. What am I missing? Script in the reply section.
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 */
 define(['N/record', 'N/search', 'N/runtime'], function (record, search, runtime) {

    function getInputData() {
        
        var savedSearchId = runtime.getCurrentScript().getParameter({ name: 'custscript_saved_search' });
        log.debug('savedSearchId', savedSearchId);
        var locationId = runtime.getCurrentScript().getParameter({ name: 'custscript_location' });
        log.debug('locationId', locationId);

        
        return search.load({ id: savedSearchId });
    }

    function map(context) {
        var result = JSON.parse(context.value);
        log.debug('Map: result', result);
        
        var journalEntryId = result.id; 
        log.debug('Map: journalEntryId', journalEntryId);

        
        var lineId = result.values.line; 
        log.debug('Map: lineId', lineId);

        
        context.write({
            key: journalEntryId,
            value: lineId 
        });
    }

    function reduce(context) {
        var journalEntryId = context.key; 
        log.debug('Reduce: journalEntryId', journalEntryId);
        var locationId = runtime.getCurrentScript().getParameter({ name: 'custscript_location' });
        log.debug('Reduce: locationId', locationId);

        
        var journalEntry = record.load({
            type: record.Type.ADV_INTER_COMPANY_JOURNAL_ENTRY,
            id: journalEntryId,
            isDynamic: true
        });
        log.debug('Reduce: journalEntry', journalEntry);

        var lineIdsToUpdate = context.values;
        log.debug('Reduce: lineIdsToUpdate', lineIdsToUpdate);

        var jeLineCount = journalEntry.getLineCount({ sublistId: 'line' });
        log.debug('Reduce: jeLineCount', jeLineCount);

        for (var i = 0; i < jeLineCount; i++) {
            var currentLineId = journalEntry.getSublistValue({
                sublistId: 'line',
                fieldId: 'line',
                line: i
            });
            log.debug('Reduce: currentLineId', currentLineId);

            if (lineIdsToUpdate.includes(currentLineId.toString())) {
                journalEntry.setSublistValue({
                    sublistId: 'line',
                    fieldId: 'location',
                    line: i,
                    value: locationId
                });
                log.debug('Reduce: Updated line', currentLineId);
            }
        }

        journalEntry.save();
    }

    function summarize(summary) {
        log.audit({
            title: 'Summary',
            details: 'Processed ' + summary.inputSummary.totalKeys
        });

        if (summary.errors) {
            log.error({
                title: 'Errors',
                details: JSON.stringify(summary.errors)
            });
        }
    }

    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce,
        summarize: summarize
    };
});
b
l
Thanks, I’ll try that.