Send Pallet Information Email when the pallet information are entered and Item Fulfillment marked as packed

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

define(["N/search","N/runtime","N/email"],

    function(search,runtime,email) {


        /**
         * @description the function to check whether a value exists in parameter
         * @param parameter -passing parameter
         * @param parameterName - passing parameter name
         * @returns{Boolean}
         */
        function checkForParameter(parameter, parameterName) {
            if (
                parameter != "" &&
                parameter != null &&
                parameter != undefined &&
                parameter != "null" &&
                parameter != "undefined" &&
                parameter != " " &&
                parameter != false
            ) {
                return true;
            } else {
                if (parameterName)
                    log.debug(
                        "Empty Value found",
                        "Empty Value for parameter " + parameterName
                    );
                return false;
            }
        }

        /***
         * Function to send pallet email
         */

        function sendEmailWithoutPallet(senderId,emailBody,emailSub,ifRecId) {
            try {
                //send email to logistics
                email.send({
                    author: senderId,
                    recipients: 1659,
                    body: emailBody,
                    subject: emailSub,
                    relatedRecords: {
                        transactionId: parseInt(ifRecId)
                    }
                });
            }catch (e) {
                log.debug("error@sendEmailWithoutPallet", e)
            }
        }
        function pageInit(scriptContext) {

        }

        function saveRecord(scriptContext) {
            try {

                var currentRecord = scriptContext.currentRecord;
                var ifRecId = currentRecord.id

                var ifHash = currentRecord.getValue({
                    fieldId: 'tranid'
                });
                var createdfromSO = currentRecord.getValue({
                    fieldId: 'createdfrom'
                });
                var shipStatus = currentRecord.getValue({
                    fieldId: 'shipstatus'
                });
                var newMemo = currentRecord.getValue({
                    fieldId: 'memo'
                });
                var fieldLookUp=search.lookupFields({
                    type: search.Type.TRANSACTION,
                    id: createdfromSO,
                    columns: ['tranid','otherrefnum']
                });
                var soHash = fieldLookUp.tranid
                var poHash = fieldLookUp.otherrefnum
                var palletEmailSend = currentRecord.getValue({
                    fieldId: 'custbody_jj_pallet_email'
                })
                var userObj = runtime.getCurrentUser();
                var senderId = userObj.id

                if ( (palletEmailSend == false) && (shipStatus=="B")) {
                    // get pallet information

                    // pallet1 details
                    var lengthOne = currentRecord.getValue({
                        fieldId: 'custbody_jj_pallet1'
                    });
                    var widthOne = currentRecord.getValue({
                        fieldId: 'custbody9'
                    });
                    var heightOne = currentRecord.getValue({
                        fieldId: 'custbody11'
                    });
                    var weightOne = currentRecord.getValue({
                        fieldId: 'custbody4'
                    })
                    var countOne = currentRecord.getValue({
                        fieldId: 'custbody3'
                    })
                    // pallet2 details
                    var lengthTwo = currentRecord.getValue({
                        fieldId: 'custbody5'
                    });
                    var widthTwo = currentRecord.getValue({
                        fieldId: 'custbody10'
                    });
                    var heightTwo = currentRecord.getValue({
                        fieldId: 'custbody12'
                    });
                    var weightTwo = currentRecord.getValue({
                        fieldId: 'custbody6'
                    });
                    var countTwo = currentRecord.getValue({
                        fieldId: 'custbody7'
                    });
                    var palletTotal = lengthOne+lengthTwo
                    var totalWidth = widthOne+widthTwo
                    var totalHeight = heightOne+heightTwo
                    var totalWeight = weightOne+ weightTwo
                    var totalCount = countOne+countTwo

                    var confMsg;

                    var emailBody = "New Pallet dimensions received, please provide BOL for " + ifHash + "/" + soHash

                    if(poHash){
                        emailBody+= "/"+poHash
                    }
                    emailBody+= " immediately - Thank you"+ '<br/><br/><br/>';

                    if((lengthOne>0) || (widthOne>0)||(heightOne>0)||(weightOne>0)||(countOne>0)||(lengthTwo>0)||(widthTwo>0)||(heightTwo>0)||(weightTwo>0)|| (countTwo>0) ){
                        // build pallet table  for email body

                        emailBody += '<table style=" border-collapse: collapse;"><tr style="height:50px;"> <th style="border:solid black;font-weight: bold; width:170px; text-align:center;">LENGTH</th><th style="border:solid black;  font-weight: bold; width:200px; text-align:center;">WIDTH</th><th style="border:solid black; font-weight: bold; width:260px; text-align:center;">HEIGHT</th><th style="border:solid black; font-weight: bold; width:260px; text-align:center;">WEIGHT</th><th style="border:solid black; font-weight: bold; width:260px; text-align:center;">CARTON COUNT ON PALLET</th></tr>';
                        if (lengthOne || widthOne || heightOne || weightOne || countOne) {
                            emailBody += '<tr style="height:40px;"><td style="border:solid black; width:170px; text-align:center;">' + lengthOne + '</td><td style="border: solid black; width:200px;text-align:center;">' + widthOne + '</td><td style="border:solid black; width:260px; text-align:center;">' + heightOne + '</td><td style="border:solid black; width:260px; text-align:center;">' + weightOne + '</td><td style="border:solid black; width:260px; text-align:center;">' + countOne + '</td></tr>';
                        }
                        if (lengthTwo || widthTwo || heightTwo || weightTwo || countTwo) {
                            emailBody += '<tr style="height:40px;"><td style="border:solid black; width:170px; text-align:center;">' + lengthTwo + '</td><td style="border: solid black; width:200px;text-align:center;">' + widthTwo + '</td><td style="border:solid black; width:260px; text-align:center;">' + heightTwo + '</td><td style="border:solid black; width:260px; text-align:center;">' + weightTwo + '</td><td style="border:solid black; width:260px; text-align:center;">' + countTwo + '</td></tr>';
                        }
                        if (palletTotal || totalWidth || totalHeight || totalWeight || totalCount) {
                            emailBody += '<tr style="height:40px;"><td style="border:solid black; width:170px; text-align:center;">' + "Total Length: " + palletTotal + '</td><td style="border: solid black; width:200px;text-align:center;">' + "Total Width:" + totalWidth + '</td><td style="border:solid black; width:260px; text-align:center;">' + "Total Height: " + totalHeight + '</td><td style="border:solid black; width:260px; text-align:center;">' + "Total Weight:" + totalWeight + '</td><td style="border:solid black; width:260px; text-align:center;">' + "Total Count:" + totalCount + '</td></tr>';
                        }
                        emailBody += '</table><br/><br/>';
                    }
                    if ( (!checkForParameter(lengthOne)) &&(!checkForParameter(widthOne))&&(!checkForParameter(heightOne))&&(!checkForParameter(weightOne))&&(!checkForParameter(countOne))&&(!checkForParameter(lengthTwo))&&(!checkForParameter(widthTwo))&&(!checkForParameter(heightTwo))&&(!checkForParameter(weightTwo))&& (!checkForParameter(countTwo))&& (newMemo) ) {

                        confMsg = confirm("Are you sure you want to send this Memo without pallet information?");
                        if((confMsg == true))  {
                            if(ifRecId) {
                                var emailSub= "New Pallet dimensions are now available - reference:" +"("+ newMemo + ") /" + ifHash + "/" + soHash
                                if(poHash){
                                    emailSub+= "/"+poHash
                                }
                                sendEmailWithoutPallet(senderId,emailBody,emailSub,ifRecId)
                                currentRecord.setValue({
                                    fieldId: 'custbody_jj_pallet_email',
                                    value: true
                                })
                            }
                        }
                        else{
                            return false
                        }
                    }
                   if((newMemo) && ((lengthOne>0) || (widthOne>0)||(heightOne>0)||(weightOne>0)||(countOne>0)||(lengthTwo>0)||(widthTwo>0)||(heightTwo>0)||(weightTwo>0)|| (countTwo>0) )) {

                    if (ifRecId){
                        var emailSub= "New Pallet dimensions are now available - reference:" +"("+ newMemo + ") /" + ifHash + "/" + soHash
                        if(poHash){
                            emailSub+= "/"+poHash
                        }
                        sendEmailWithoutPallet(senderId,emailBody,emailSub,ifRecId)

                        currentRecord.setValue({
                           fieldId: 'custbody_jj_pallet_email',
                           value: true
                       })
                   }
                    }
                    if ( (!newMemo) && ((lengthOne>0) || (widthOne>0)||(heightOne>0)||(weightOne>0)||(countOne>0)||(lengthTwo>0)||(widthTwo>0)||(heightTwo>0)||(weightTwo>0)|| (countTwo>0))){
                        log.debug("pass pass")
                        confMsg = confirm("Are you sure you want to send these dimensions without reference # in the MEMO");
                        if (confMsg == true) {
                            if(ifRecId) {
                                var emailSub= "New Pallet dimensions are now available - " + ifHash + "/" + soHash
                                if(poHash){
                                    emailSub+= "/"+poHash
                                }
                                sendEmailWithoutPallet(senderId,emailBody,emailSub,ifRecId)


                                currentRecord.setValue({
                                    fieldId: 'custbody_jj_pallet_email',
                                    value: true
                                })
                            }

                        }else{
                            return false
                        }
                    }
                }

                return true;

            }catch (e) {
                log.error("error@saveRecord",e)
            }
        }

        return {
            pageInit: pageInit,
            // fieldChanged:fieldChanged,
            saveRecord: saveRecord
        };

    });

Leave a comment

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