Email notification on Stock Movement

  • CSV import for Bin Owner field Updation on Bin records in production.
  • A User event script after submit is deployed on the creation of Bin Transfer and Inventory Transfer record. 
  • The script will send an email to all the Bin Owners of the From Bin to notify them that the items are transferred to another bin through the mentioned Transaction.
  • Alert won’t be generated: 
  1. if Checkbox “Exclude for alert” is checked
  2. If the location is and the owner is :   
  •  A_Lafayette – MTI : Mohan Caldera
  •  A_Lafayette – MAC : Mohan Caldera
  •  A_Ingolstadt – MTE : Dominic Schmidt
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*************************************************************************************************************************
 * 
 * Email notification on Stock Movement
 * **********************************************************************************************************************
 * 
 * Script Description : The script will send an email to all the Bin Owners of the From Bin to notify them that the items are transferred to another bin through the mentioned Transaction.
 *
 *
**************************************************************************************************************************/

define(['N/record', 'N/email', 'N/search', 'N/runtime'],

    function (record, email, search, runtime) {


        function afterSubmit(scriptContext) {
            try {

                var newRec = scriptContext.newRecord;
                //log.debug("newRec", newRec);

                var recId = newRec.id;
                //log.debug("record Id", recId);

                var recType = newRec.type;
                //log.debug("record Type", recType);

                if (recType == "bintransfer" || recType == "inventorytransfer") {
                    if (scriptContext.type == "create") {

                        transactionResObj = {};
                        var transactionSearchObj = search.create({
                            type: "transaction",
                            filters:
                                [
                                    [["type", "anyof", "InvTrnfr", "BinTrnfr"], "AND", ["internalidnumber", "equalto", recId], "AND", ["mainline", "is", "F"], "AND", ["inventorydetail.itemcount", "notgreaterthan", "0"], "AND", ["formulanumeric: case when ({inventorydetail.binnumber} = {binnumber.binnumber}) then 1 else 0 end", "equalto", "1"], "AND", ["formulanumeric: case when ({location}={binnumber.location}) then 1 else 0 end", "equalto", "1"], "AND", ["binnumber.inactive", "is", "F"], "AND", ["binnumber.custrecord_jj_is_exclude_for_alert", "is", "F"]],
                                    "AND",
                                    "NOT",
                                    [["location", "anyof", "1"], "AND", ["binnumber.custrecord_jj_bin_owner", "anyof", "2957"]],
                                    "AND",
                                    "NOT",
                                    [["location", "anyof", "23"], "AND", ["binnumber.custrecord_jj_bin_owner", "anyof", "2957"]],
                                    "AND",
                                    "NOT",
                                    [["location", "anyof", "9"], "AND", ["binnumber.custrecord_jj_bin_owner", "anyof", "33749"]]
                                ],
                            columns:
                                [
                                    search.createColumn({
                                        name: "custrecord_jj_bin_owner",
                                        join: "binNumber",
                                        label: "Bin Owner"
                                    }),
                                    search.createColumn({ name: "item", label: "Item" }),
                                    search.createColumn({
                                        name: "binnumber",
                                        join: "inventoryDetail",
                                        label: "Bin Number"
                                    }),
                                    search.createColumn({
                                        name: "quantity",
                                        join: "inventoryDetail",
                                        label: "Quantity"
                                    }),
                                    search.createColumn({
                                        name: "location",
                                        join: "binNumber",
                                        label: "Location"
                                    }),
                                    search.createColumn({ name: "transactionnumber", label: "Transaction Number" }),
                                    search.createColumn({ name: "tranid", label: "Document Number" }),
                                    search.createColumn({
                                        name: "internalid",
                                        join: "binNumber",
                                        label: "Internal ID"
                                    })

                                ]
                        });
                        var searchResultCount = transactionSearchObj.runPaged().count;
                        //log.debug("transactionSearchObj result count", searchResultCount);

                        transactionSearchObj.run().each(function (result) {
                            var binOwnerID = result.getValue(transactionSearchObj.columns[0]);
                            if (transactionResObj[binOwnerID]) {
                                transactionResObj[binOwnerID].push({
                                    binOwnerName: result.getText(transactionSearchObj.columns[0]),
                                    itemName: result.getText(transactionSearchObj.columns[1]),
                                    binNo: result.getText(transactionSearchObj.columns[2]),
                                    quantity: result.getValue(transactionSearchObj.columns[3]),
                                    locationName: result.getText(transactionSearchObj.columns[4]),
                                    tranNo: result.getValue(transactionSearchObj.columns[5]),
                                    DocNo: result.getValue(transactionSearchObj.columns[6])
                                })

                            } else {
                                transactionResObj[binOwnerID] = [{
                                    binOwnerName: result.getText(transactionSearchObj.columns[0]),
                                    itemName: result.getText(transactionSearchObj.columns[1]),
                                    binNo: result.getText(transactionSearchObj.columns[2]),
                                    quantity: result.getValue(transactionSearchObj.columns[3]),
                                    locationName: result.getText(transactionSearchObj.columns[4]),
                                    tranNo: result.getValue(transactionSearchObj.columns[5]),
                                    DocNo: result.getValue(transactionSearchObj.columns[6])

                                }]
                            }

                            return true;
                        });

                        var senderID = runtime.getCurrentUser().id;
                        for (var key in transactionResObj) {

                            var valuesArr = transactionResObj[key];

                            //build the HTML table header
                            var msgHTML = '<table width=100%" class="listtable listborder uir-list-table" style="position: relative;" ><tr class="uir-machine-headerrow">';
                            msgHTML += '<td class="listheadertdleft listheadertextb uir-column-large">DOCUMENT NUMBER</td>'
                            msgHTML += '<td class="listheadertdleft listheadertextb uir-column-large">TRANSACTION NUMBER</td>'
                            msgHTML += '<td class="listheadertdleft listheadertextb uir-column-large">LOCATION</td>'
                            msgHTML += '<td class="listheadertdleft listheadertextb uir-column-large">ITEM</td>'
                            msgHTML += '<td class="listheadertdleft listheadertextb uir-column-large">BIN NUMBER</td>'
                            msgHTML += '<td class="listheadertdleft listheadertextb uir-column-large">QUANTITY</td>'



                            //spin through results and draw html table row/cell elements
                            for (var i = 0; i < valuesArr.length; i++) {

                                var msgRow = (i % 2) ? 'even' : 'odd';
                                msgHTML += '</tr><tr class="uir-list-row-tr uir-list-row-' + msgRow + '">';
                                msgHTML += '<td class="uir-list-row-cell listtext">' + valuesArr[i].DocNo + '</td>';
                                msgHTML += '<td class="uir-list-row-cell listtext">' + valuesArr[i].tranNo + '</td>';
                                msgHTML += '<td class="uir-list-row-cell listtext">' + valuesArr[i].locationName + '</td>';
                                msgHTML += '<td class="uir-list-row-cell listtext">' + valuesArr[i].itemName + '</td>';
                                msgHTML += '<td class="uir-list-row-cell listtext">' + valuesArr[i].binNo + '</td>';
                                msgHTML += '<td class="uir-list-row-cell listtext">' + valuesArr[i].quantity + '</td>';
                                var binOwner = valuesArr[i].binOwnerName
                            }
                            msgHTML += '</tr></table>'

                            email.send({
                                author: senderID,
                                recipients: key,//[customerEmail],
                                subject: 'An Alert of stock movement',
                                body: '<BR />Hi ' + binOwner + ',<BR/> <BR/>This email is to notify you that bin transfer is initiated. Please find more details regarding the transaction below.' + '<BR/> <BR/>' + msgHTML + '<br /><br /> Thank You'
                            })

                        }

                    }

                }

            }

            catch (e) {
                log.debug("error@record creation", e.message);
                log.error("error@record creation", e.message);
            }

        }
        return {
            afterSubmit: afterSubmit
        };
    });


Leave a comment

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