Sceduled script for remainders on based on Due Date

/**

 * @NApiVersion 2.1

 * @NScriptType MapReduceScript

 */

define([‘N/record’, ‘N/search’, ‘N/email’],

    /**

 * @param{record} record

 * @param{search} search

 * @param{email} email

 */

    (record, search, email) => {

        /**

         * Defines constant sender employee

         */

        const SENDER = ‘2094’;

        /**

         * Function to get the case records

         * @returns {}

         */

        function getCaseRecords() {

            try {

                let supportcaseSearchObj = search.create({

                    type: “supportcase”,

                    filters:

                        [

                            [“status”, “noneof”, “5”],

                            “AND”,

                            [“isinactive”, “is”, “F”],

                            “AND”,

                            [“formulanumeric: CASE WHEN ROUND({today} – {custevent_jj_due_date_atpl104}) > = 1 THEN 1 ELSE 0 END”, “equalto”, “1”],

                            “AND”,

                            [“custevent_jj_case_pending”, “isempty”, “”]

                        ],

                    columns:

                        [

                            search.createColumn({ name: “internalid”, label: “Internal ID” }),

                            search.createColumn({ name: “assigned”, label: “Assigned To” }),

                            search.createColumn({

                                name: “casenumber”,

                                sort: search.Sort.ASC,

                                label: “Number”

                            }),

                            search.createColumn({ name: “custevent_jj_company_atpl104”, label: “Company” }),

                        ]

                });

                let searchResultCount = supportcaseSearchObj.runPaged().count;

                if (searchResultCount > 0) {

                    return supportcaseSearchObj

                }

                else {

                    return {}

                }

            }

            catch (err) {

                log.error(‘error@getCaseRecords’, err)

                return {}

            }

        }

        /**

         * Function to get the supervisor

         * @param {*} requestor

         * @returns

         */

        function getSupervisor(requestor) {

            try {

                let employeeSearchObj = search.create({

                    type: “employee”,

                    filters:

                        [

                            [“internalid”, “anyof”, requestor],

                            “AND”,

                            [“isinactive”, “is”, “F”]

                        ],

                    columns:

                        [

                            search.createColumn({ name: “firstname”, label: “First Name” }),

                            search.createColumn({ name: “supervisor”, label: “Supervisor” })

                        ]

                });

                let searchResultCount = employeeSearchObj.runPaged().count;

                let empObj = {}

                if (searchResultCount > 0) {

                    employeeSearchObj.run().each(function (result) {

                        empObj.name = result.getValue({

                            name: “firstname”, label: “First Name”

                        })

                        empObj.supervisor = result.getValue({

                            name: “supervisor”, label: “Supervisor”

                        })

                        return true;

                    });

                    return empObj

                }

                else {

                    return {}

                }

            }

            catch (err) {

                log.error(“error@getSupervisor”, err)

                return {}

            }

        }

        /**

         * Function to get the Email Address

         * @param {*} requestor

         * @returns

         */

        function getEmailAddress(requestor) {

            try {

                let employeeSearchObj = search.create({

                    type: “employee”,

                    filters:

                        [

                            [“isinactive”, “is”, “F”],

                            “AND”,

                            [“internalid”, “anyof”, requestor]

                        ],

                    columns:

                        [

                            search.createColumn({ name: “internalid”, label: “Internal ID” }),

                            search.createColumn({ name: “email”, label: “Email” })

                        ]

                });

                let searchResultCount = employeeSearchObj.runPaged().count;

                let empObj = {}

                if (searchResultCount > 0) {

                    employeeSearchObj.run().each(function (result) {

                        let employee = result.getValue({

                            name: “internalid”, label: “Internal ID”

                        })

                        empObj[employee] = result.getValue({

                            name: “email”, label: “Email”

                        })

                        return true;

                    });

                    return empObj

                }

                else {

                    return {}

                }

            }

            catch (err) {

                log.error(“error@getEmailAddress”, err)

                return {}

            }

        }

        /**

         * Function to send Reason Email

         * @param {*} emailData

         * @param {*} emailAddress

         */

        function sendReasonEmail(emailData, emailAddress) {

            try {

                log.debug(“emailData”, emailData)

                let emailMessage = ‘The due date for completing the help desk request ‘ + emailData.caseNumber + ‘  for the employee ‘ + emailData.requestor + ‘ is expired and the help desk is not completed. Please provide the reason for the help desk is not completed.’

                let emailBody = `Hi ${emailData.assigneeName},<br/> <br/>` + emailMessage + `<br/>`

                emailBody += `<br/>Thank you`;

                let supervisorArray = []

                let supervisor = emailData.supervisor

                let assignee = emailData.assignee

                supervisorArray.push(emailAddress[supervisor])

                log.debug(“supervisorArray”, supervisorArray)

                email.send({

                    author: SENDER,

                    recipients: emailAddress[assignee],

                    cc: supervisorArray,

                    subject: ‘Help Desk Pending Reason Submission for ‘ +  emailData.caseNumber,

                    body: emailBody,

                });

            }

            catch (err) {

                log.error(“error@sendReaminders”, err)

            }

        }

        /**

         * Defines the function that is executed at the beginning of the map/reduce process and generates the input data.

         * @param {Object} inputContext

         * @param {boolean} inputContext.isRestarted – Indicates whether the current invocation of this function is the first

         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)

         * @param {Object} inputContext.ObjectRef – Object that references the input data

         * @typedef {Object} ObjectRef

         * @property {string|number} ObjectRef.id – Internal ID of the record instance that contains the input data

         * @property {string} ObjectRef.type – Type of the record instance that contains the input data

         * @returns {Array|Object|Search|ObjectRef|File|Query} The input data to use in the map/reduce process

         * @since 2015.2

         */

        const getInputData = (inputContext) => {

            try {

                let caseRecords = getCaseRecords()

                return caseRecords

            }

            catch (err) {

                log.error(“error@getInputData”, err)

                return {}

            }

        }

        /**

         * Defines the function that is executed when the reduce entry point is triggered. This entry point is triggered

         * automatically when the associated map stage is complete. This function is applied to each group in the provided context.

         * @param {Object} reduceContext – Data collection containing the groups to process in the reduce stage. This parameter is

         *     provided automatically based on the results of the map stage.

         * @param {Iterator} reduceContext.errors – Serialized errors that were thrown during previous attempts to execute the

         *     reduce function on the current group

         * @param {number} reduceContext.executionNo – Number of times the reduce function has been executed on the current group

         * @param {boolean} reduceContext.isRestarted – Indicates whether the current invocation of this function is the first

         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)

         * @param {string} reduceContext.key – Key to be processed during the reduce stage

         * @param {List<String>} reduceContext.values – All values associated with a unique key that was passed to the reduce stage

         *     for processing

         * @since 2015.2

         */

        const reduce = (reduceContext) => {

            try {

                let dataObj = reduceContext.values.map(JSON.parse);

                let assignee = dataObj[0].values[“assigned”].value

                let supervisorData = getSupervisor(assignee)

                let empArray = [];

                empArray.push(assignee, supervisorData.supervisor)

                let emailAddress = getEmailAddress(empArray)

                let emailData = {}

                emailData.assignee = assignee

                emailData.assigneeName = dataObj[0].values[“assigned”].text,

                    emailData.supervisor = supervisorData.supervisor,

                    emailData.caseNumber = dataObj[0].values[“casenumber”],

                    emailData.requestor = dataObj[0].values[“custevent_jj_company_atpl104”].text

                let notification = sendReasonEmail(emailData, emailAddress)

            }

            catch (err) {

                log.error(“error@reduce”, err)

            }

        }

        return { getInputData, reduce, }

    });

Leave a comment

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