workflow action script to send emails for approval in role-based stages

/**

 * @NApiVersion 2.1

 * @NScriptType WorkflowActionScript

 */

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

 /**

 * @param{record} record

 * @param{search} search

 * @param{runtime} runtime

 */

    (record, search, runtime, email) => {

        “use strict”;

        /*

         * This function fetches the internal IDs of employees associated with a specific role.

         *

         * @param {string} role – Internal ID of the role to filter employees.

         * @returns {Array} – An array of internal IDs of employees.

         */

        function getEmployeeData(role) {

            try {

                let internalIdArray = [];                

                let employeeSearchObj = search.create({

                    type: “employee”,

                    filters:

                        [

                            [“role”, “anyof”, role]

                        ],

                    columns:

                        [

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

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

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

                        ]

                });

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

                    internalIdArray.push(result.getValue({ name: “internalid” }));

                    return true;

                });

                return internalIdArray;

            } catch (e) {

                log.error(‘error @ getEmployeeData’, e);

                return [];

            }

        }

        /**

         * Defines the WorkflowAction script trigger point.

         * @param {Object} scriptContext

         * @param {Record} scriptContext.newRecord – New record

         * @param {Record} scriptContext.oldRecord – Old record

         * @param {string} scriptContext.workflowId – Internal ID of workflow which triggered this action

         * @param {string} scriptContext.type – Event type

         * @param {Form} scriptContext.form – Current form that the script uses to interact with the record

         * @since 2016.1

         */

        const onAction = (scriptContext) => {

            const scriptObj = runtime.getCurrentScript();

            const userObj = runtime.getCurrentUser();

            const deanArray = scriptObj.getParameter({ name: ‘custscript_jj_tujdean_role’ });

            const cfoArray = scriptObj.getParameter({ name: ‘custscript_jj_tujcfo_role’ });

            const submission = {

                estimate: scriptObj.getParameter({ name: ‘custscript_jj_submission_estimate’ }),

                invoice: scriptObj.getParameter({ name: ‘custscript_jj_submission_invoice’ }),

            }

            try {

                let currentRecordObj = scriptContext.newRecord;

                let docNumer = currentRecordObj.getValue({

                    fieldId: ‘tranid’

                });

                let submissionType = currentRecordObj.getValue({

                    fieldId: ‘custbody_jj_submission_type’

                });

                let cfoApproval = currentRecordObj.getValue({

                    fieldId: ‘custbody_jj_cfo_approval’

                });              

                let deanApproval = currentRecordObj.getValue({

                    fieldId: ‘custbody_jj_dean_approval’

                });                              

                let employeeArray = [];

                if (cfoApproval === true || cfoApproval === ‘T’) {

                    employeeArray = getEmployeeData(cfoArray);

                }

                else if (deanApproval === true || deanApproval === ‘T’) {

                    employeeArray = getEmployeeData(deanArray);

                }

                else

                    employeeArray = [];

                if (employeeArray.length > 0) {                  

                    if (submissionType === submission.estimate) {

                        email.send({

                            author: userObj.id,

                            recipients: employeeArray,

                            subject: “Purchase Approval Request: Purchase Order “ + docNumer,

                            body: ‘Hi <br /> Please review and approve the Purchase Order <strong>’ + docNumer + ‘</strong>.<br />’ +

                                ‘Thank you!<br />’ +

                                ‘<a href=”https://448734-rp.app.netsuite.com/app/accounting/transactions/purchord.nl?id=’ +

                                currentRecordObj.id + ‘” target=”_blank”>View Record</a>’,

                            relatedRecords: {

                                transactionId: currentRecordObj.id

                            }

                        });

                    }

                    if (submissionType === submission.invoice) {

                        email.send({

                            author: userObj.id,

                            recipients: employeeArray,

                            subject: “Payment Approval Request: Purchase Order “ + docNumer,

                            body: ‘Hi <br /> Please review and approve the Purchase Order <strong>’ + docNumer + ‘</strong>.<br />’ +

                                ‘Thank you!<br />’ +

                                ‘<a href=”https://448734-rp.app.netsuite.com/app/accounting/transactions/purchord.nl?id=’ +

                                currentRecordObj.id + ‘” target=”_blank”>View Record</a>’,

                            relatedRecords: {

                                transactionId: currentRecordObj.id

                            }

                        });

                    }

                }

                else {

                    log.debug(‘No Employee Found’, ‘The “Approved/Rejected By” field is empty.’);

                }

            } catch (error) {

                log.error(‘Error Retrieving Employee Role’, error.message);

            }

        }

        return { onAction };

    });

Leave a comment

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