Map reduce script to send picking ticket based on item line location.

if there is 5 items in a sales order with item line location A, B. 3 items in location B and 2 items in location A. need to send details of 3 items in B-to-B location. and other 2 items to A. for this create two advanced pdf templates for picking ticket give the filter condition only for A and only for B. use these templates form id here.

 function getInputData(context) {
            try {               

log.debug(‘==START==’, ‘==START==’); 

              var scriptObj = runtime.getCurrentScript(); 

              //var mdiLocation = scriptObj.getParameter({name: ‘custscript_mdi_location1’}); 

              var transactionSearchObj = search.create({ 

                  type: “transaction”, 

                  filters:  [ 

                          [“status”, “anyof”, “SalesOrd:E”, “SalesOrd:B”, “SalesOrd:D”], “AND”,                            [“custbody_email_sent_uis”, “is”, “F”],                            “AND”, 

                          [“location”, “anyof”, “53”, “13”],                            “AND”, 

                          [“mainline”, “is”, “F”],                            “AND”,   

                        [“trandate”, “onorafter”, “1/21/2022”],                            “AND”, 

                          [“cogs”, “is”, “F”],                            “AND”,

                            [“taxline”, “is”, “F”],                            “AND”, 

                          [“shipping”, “is”, “F”]                        ], 

                  columns:                        [   

                        search.createColumn({ name: “item”, label: “Item” }),

                          search.createColumn({ name: “quantity”, label: “Quantity” }), 

                          search.createColumn({ name: “location”, label: “Location” }),   

                        search.createColumn({ name: “tranid”, label: “Document Number” })                   

    ]               

});
                return transactionSearchObj;
            }            catch (e) { 

              log.error(‘Error’, JSON.stringify(e)); 

          }
        }

 function reduce(context) {
            try { 

                var scriptObj = runtime.getCurrentScript();

                var pickTicketFromEmployee = scriptObj.getParameter({ name: ‘custscript_pick_ticket_from1’ }); 

              var reduceKey = context.key; 

              var reduceValues = context.values; 

              var msdFlag = 0; 

              var ibFlag = 0; 

              var tid;  //tranid
                reduceValues.forEach(function (jsonString) { 

                  var object = JSON.parse(jsonString); 

                  tid = object.tranid;
                    if (object.location == 13)

                      msdFlag = 1; 

                  }   

                if (object.location == 53)

                      ibFlag = 1; 

                  }                });
                var formsId, createdFile;   

            if (msdFlag == 1) { 

            var pickTicketEmail = scriptObj.getParameter({ name: ‘custscript_pick_ticket_email1’ }); 

                  formsId = 167;  // picking ticket form internalid 

                  createdFile = createFile(pickTicketFromEmployee, pickTicketEmail, formsId, reduceKey, tid);                }
                if (ibFlag == 1)

{   

                var pickTicketEmail = scriptObj.getParameter({ name: ‘custscript_pick_ticket_emailinternation1’ });                    formsId = 168; //picking ticket form internalid 

                  createdFile = createFile(pickTicketFromEmployee, pickTicketEmail, formsId, reduceKey, tid); 

              }
                if (createdFile)

{                   

record.submitFields({ 

                      type: record.Type.SALES_ORDER, 

                      id: context.key,   

                    values: { 

                          custbody_email_sent_uis: true

                        },   

                    options:

{                           

enableSourcing: false,   

                        ignoreMandatoryFields: true 

                      }                    });   

            }         

  }            catch (e) {

                error = JSON.stringify(e);   

            log.error(‘Error in Reduce stage’, error); 

          }        }

  function summarize(summary) {   

        log.debug(‘==END==’, ‘==END==’);   

    }
        return {           

getInputData: getInputData,   

        map: map, 

          reduce: reduce,   

        summarize: summarize 

    };

  function map(context) {         
            try {
                var salesorderDetails = JSON.parse(context.value); 

              var mapKey = salesorderDetails.id ;

              var location = salesorderDetails.values.location.value; 

              var tranid = salesorderDetails.values.tranid;
                const details = {}; 

              details.location = location; 

              details.tranid = tranid;   

            context.write({     

              key: mapKey, 

                  value: details     

          });            } 

          catch (e) {   

            error = JSON.stringify(e); 

              log.error(‘Error in MAP stage’, error);     

      }        }

function createFile(fromId, toId, fid, redkey, traid) { 

          try {
                var transactionFile = render.pickingTicket({

                    entityId: parseInt(redkey), 

                  printMode: render.PrintMode.PDF, 

                  formId: fid, 

                  inCustLocale: true

                });


                email.send({ 

                  author: fromId,

                    recipients: [toId],

                    subject: ‘Pick Ticket for Sales Order ‘ + traid, 

                  body: ‘Attached.’, 

                  attachments: [transactionFile], 

                  relatedRecords: { 

                      transactionId: parseInt(redkey) 

                  }                });
                return true;
            }           

catch (e) {   

            log.error(‘Error@ email’, e); 

            return false;     

      }   

    }

             


               

Leave a comment

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