Map Reduced Sript Example

/**
 * @NApiVersion 2.0
 * @NScriptType MapReduceScript
 */
define(['N/search', 'N/record', 'N/email', 'N/runtime', 'N/error'],
    function(search, record, email, runtime, error)
    {
        function sendFinalEmail(noOfRec)
        {
          

            var author = -5;//internal id of employee
            var recipients = 'Enter Email id here';
            var subject = 'Map/Reduce script processed '+ noOfRec+'records';
            var body = 'enter you email body here.';

            email.send({
                author: author,
                recipients: recipients,
                subject: subject,
                body: body
            });
        }

      

        function updateInvoice(recordId)
        {
            // you can use body fields or line field as per need.
            //to keep it simple we are using memo field
            var invoice = record.load({
                type: record.Type.INVOICE,
                id: recordId,
                isDynamic: true
            });

            invoice.setText({
                fieldId: 'memo',
                value: 'updated By Map Reduced.'
            });

         
            invoice.save();
        }

        function getInputData()
        {
            return search.create({
                type: record.Type.INVOICE,
                filters: [['memo', search.Operator.IS, ''], 'AND', ['mainline', search.Operator.IS, 'T']],
                columns: ['entity'],
                title: 'Empty Invoice memo Search'
            });
        }

        function map(context)
        {
            var searchResult = JSON.parse(context.value);
            var invoiceId = searchResult.id;
            var entityId = searchResult.values.entity.value;

            updateInvoice(invoiceId);

            context.write({
                key: entityId,
                value: invoiceId
            });

        }

        function reduce(context)
        {
            var customerId = context.key;
            var cInvoiceId = context.value;
  
            var rec = record.create({
                    type: 'customrecord_mappingrecord',
                });

                rec.setValue({
                    fieldId : 'name',
                    value: customerId+' ' +cInvoiceId
                });

                rec.setValue({
                    fieldId: 'custrecord_invoice',
                    value: cInvoiceId
                });
                rec.setValue({
                    fieldId: 'custrecord_customerid',
                    value: customerId
                });
            var mappingRec = rec.save();

            context.write({
                key: mappingRec
            });
        }

        function summarize(summary)
        {
            var noOfProcessedRecord = 0;
             context.output.iterator().each(function () { noOfProcessedRecord++; });
             log.debug({ title: 'No of record processed', details: noOfProcessedRecord });
             sendFinalEmail(noOfProcessedRecord);
          
        }

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

Comments