Notify sales order Dispatch

A custom process to update the sales manager about the unfulfilled, unpaid orders. A scheduled script which runs every day checks the sales orders which are partially fulfilled or not fulfilled. An email template is loaded and these orders are aligned and updated and send to corresponding sales managers.

Tempalte

<!DOCTYPE html>
<html>

<head>
    <style>
    #tablecontent {
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        border-collapse: collapse;
        width: 35%;
    }

    #tablecontent td,
    #tablecontent th {
        border: 1px solid #ddd;
        padding: 8px;
        text-align: center;
    }

    #tablecontent tr:nth-child(even) {
        background-color: #f2f2f2;
        text-align: center;
    }

    #tablecontent tr:hover {
        background-color: #ddd;
    }

    #tablecontent th {
        padding-top: 12px;
        padding-bottom: 12px;
        text-align: center;
        background-color: #3b5998;
        color: white;
    }
    </style>
</head>

<body>
    <h1>SO's not picked, with Stock on Hand</h1>
    <table id="tablecontent">
        <tr>
            <th>#</th>
            <th>Sales Orders</th>
            <th>Customers</th>
        </tr>
        <tr>
            <!--<td><a href="<-Replace->"> <-ReplaceWithSOName-></a></td>-->
            <-ReplacewithURL->
        </tr>
    </table>
</body>

</html>

Schedule Script

/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
define(['N/email', 'N/file', 'N/https', 'N/record', 'N/runtime', 'N/search', 'N/url'],
    /**
     * @param {email} email
     * @param {http} http
     * @param {https} https
     * @param {record} record
     * @param {runtime} runtime
     * @param {search} search
     * @param {url} url
     */
    function(email, file, https, record, runtime, search, url) {

        /**
         * Definition of the Scheduled script trigger point.
         *
         * @param {Object} scriptContext
         * @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
         * @Since 2015.2
         */
        var main = {
            execute: function(scriptContext) {

                var soDetails = main.fetchSalesorders();
                log.debug("soDetails", soDetails);
                main.createContent(soDetails);

            },
            fetchSalesorders: function() {
                var transactionSearchObj = search.create({
                    type: "transaction",
                    filters: [
                        [
                            ["type", "anyof", "SalesOrd"], "AND", ["printedpickingticket", "is", "T"], "AND", ["status", "noneof", "SalesOrd:C", "VendBill:B", "SalesOrd:H"], "AND", ["datecreated", "notonorafter", "yesterday"], "AND", ["status", "anyof", "SalesOrd:D", "SalesOrd:B"]
                        ],
                        "AND", [
                            [
                                ["item.type", "noneof", "Assembly"], "AND", ["item.quantityonhand", "greaterthan", "0"]
                            ], "OR", ["item.type", "anyof", "Assembly"]
                        ]
                    ],
                    columns: [
                        search.createColumn({
                            name: "tranid",
                            summary: "GROUP",
                            label: "Document Number"
                        }),
                        search.createColumn({
                            name: "internalid",
                            summary: "GROUP",
                            label: "Internal ID"
                        }),
                        search.createColumn({
                            name: "altname",
                            join: "customer",
                            summary: "GROUP",
                            label: "Name"
                        })
                    ]
                });
                var searchResultCount = transactionSearchObj.runPaged().count;
                var soArray = [];
                transactionSearchObj.run().each(function(result) {
                    soArray.push({
                        internalId: result.getValue({
                            name: "internalid",
                            summary: "GROUP"
                        }),
                        tranid: result.getValue({
                            name: "tranid",
                            summary: "GROUP"
                        }),
                        customer: result.getValue({
                            name: "altname",
                            join: "customer",
                            summary: "GROUP"
                        })
                    });
                    return true;
                });
                return soArray;
            },
            createContent: function(dataArray) {
                log.debug("dataArray", dataArray);
                var content;
                var fileObj = file.load({
                    id: 360973
                });
                if (fileObj.size < 10485760) {
                    var Template = fileObj.getContents();
                }

                var salesrow = "";

                var j = 1;
                for (var i = 0; i < dataArray.length; i++) {
                    var url = "https://system.na2.netsuite.com/app/accounting/transactions/salesord.nl?id=<replaceurls>&whence="
                    var url = url.replace("<replaceurls>", dataArray[i].internalId)
                    salesrow += '<tr><td>' + j + '</td>'
                    salesrow += '<td><a href = "' + url + '" > ' + dataArray[i].tranid + ' </a></td>'
                    salesrow += '<td>' + dataArray[i].customer + ' </td></tr>'
                    j++
                }

                content = Template.replace("<-ReplacewithURL->", salesrow);
                var fileObj = file.create({
                    name: 'Dispatchtemplate.html',
                    fileType: file.Type.HTMLDOC,
                    contents: content,
                    encoding: file.Encoding.UTF8,
                    folder: '-10',
                    isOnline: true
                });
                var fileid = fileObj.save();
                if (dataArray.length > 0)
                    main.sendemail(content);
            },
            sendemail: function(content) {
                //   recipients: "edwin@jobinandjismi.com",
                email.send({
                    author: "-5",
                    recipients: "despatch@hygienetech.co.nz",
                    subject: 'Picking Ticket',
                    body: content
                });
            },


        }
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };
        return main;

    });

Leave a comment

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