Email sending on save action of task record

Requirement

When a task record is opened, in the escalation tab, when we enter data in the escalation record and save the task record then send an email to the employee in the escalation record.

When task record is saved, the escalation record needs to become empty.

Solution

A userevent script is created to send email on the save action of the task record. When the record is saved, email will be send to corresponding email address in the escalation records. The email send will have the email body from the ESCALATION MESSAGE field.

The email sender will be the current user. The email receiver will be the email id from the escalation record.

The email subject will be hardcoded inside the script. On the save of task record, the ESCALATION MESSAGE field and corresponding escalate to records will be deleted.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */


define(['N/currentRecord', 'N/email', 'N/record', 'N/search', 'N/runtime'],
    /**
     * @param{currentRecord} currentRecord
     * @param{email} email
     * @param{record} record
     * @param{search} search
     * @param{runtime} runtime
     */
    (currentRecord, email, record, search, runtime) => {


        const afterSubmit = (scriptContext) => {

            try {

                //Get the current record
                var currentRec = scriptContext.newRecord

                //Calling the taskSearch
                var searchResult = taskSearch(currentRec.id)

                //Get the current User
                var user = runtime.getCurrentUser();
                var msg
                var receiver

                //Getting the email message and the receiver
                for (var i = 0; i < searchResult.length; i++) {
                    id = searchResult[i].getValue({name: "internalid", label: "Internal ID"});
                    msg = searchResult[i].getValue({
                        name: "custevent_jj_escalations_message",
                        label: "Escalation Message"
                    });
                    receiver = searchResult[i].getValue({
                        name: "custrecord_jj_emlpoyee_email_id",
                        join: "CUSTRECORD_JJ_LINK_PARENT",
                        label: "Employee Email ID"
                    });

                    var employee=searchResult[i].getValue({
                        name: "custrecord_jj_employee_task_test",
                        join: "CUSTRECORD_JJ_LINK_PARENT",
                        label: "Escalatee"
                    });
                    var link=searchResult[i].getValue({
                        name: "formulatext",
                        formula: "'<a href=\"https://6687177-sb1.app.netsuite.com/app/crm/calendar/task.nl?id='||{internalid}||'\" target=\"_blank\">'||{title}||'</a>'",
                        label: "Formula (Text)"
                    });

              
                    var empName=search.lookupFields({
                        type: search.Type.EMPLOYEE,
                        id: employee,
                        columns: ['entityid']
                    }).entityid;
                    //Get the task title
                    var title = searchResult[i].getValue({name: "title", label: "Task Title"});
                    var userId = user.id

                    if (checkForParameter(msg) && checkForParameter(receiver)) {
                        //Call sendemail function
                        sendEmail(userId, msg, receiver, title,empName,link)
                    }
                }

                for (var j = searchResult.length - 1; j >= 0; j--) {
                    var custRecId = searchResult[j].getValue({
                        name: "internalid",
                        join: "CUSTRECORD_JJ_LINK_PARENT",
                        label: "Internal ID"
                    });

                    if(custRecId){
                        var custRecord = record.delete({
                            type: 'customrecord_jj_escalate_to',
                            id: custRecId,
                        });
                    }

                }
                //Setting the escalation message to empty
                var id = record.submitFields({
                    type: record.Type.TASK,
                    id: currentRec.id,
                    values: {
                        custevent_jj_escalations_message: ''
                    },
                    options: {
                        enableSourcing: false,
                        ignoreMandatoryFields: true
                    }
                });


            } catch (e) {
                log.debug('Error@AfterSubmit', e)
            }
        }

        //Saved search created on task record
        function taskSearch(taskId) {
            try {
                var taskSearchObj = search.create({
                    type: "task",
                    filters:
                        [
                            ["internalid", "anyof", taskId]
                        ],
                    columns:
                        [
                            search.createColumn({name: "internalid", label: "Internal ID"}),
                            search.createColumn({
                                name: "custevent_jj_escalations_message",
                                label: "Escalation Message"
                            }),
                            search.createColumn({
                                name: "custrecord_jj_emlpoyee_email_id",
                                join: "CUSTRECORD_JJ_LINK_PARENT",
                                label: "Employee Email ID"
                            }),
                            search.createColumn({
                                name: "internalid",
                                join: "CUSTRECORD_JJ_LINK_PARENT",
                                label: "Internal ID"
                            }),
                            search.createColumn({name: "title", label: "Task Title"}),
                            search.createColumn({
                                name: "custrecord_jj_employee_task_test",
                                join: "CUSTRECORD_JJ_LINK_PARENT",
                                label: "Escalatee"
                            }),
                            search.createColumn({
                                name: "formulatext",
                                formula: "'<a href=\"https://6687177-sb1.app.netsuite.com/app/crm/calendar/task.nl?id='||{internalid}||'\" target=\"_blank\">'||{title}||'</a>'",
                                label: "Formula (Text)"
                            })
                        ]
                });
                var searchResult = taskSearchObj.run().getRange({
                    start: 0,
                    end: 1000
                });

                return searchResult;

            } catch (e) {
                log.debug('Error@taskSearch', e)
            }
        }

        //Function to send email
        function sendEmail(userId, msg, receiver, title,empName,link) {
            try {
                email.send({
                    author: userId,
                    recipients: receiver,
                    subject: 'New Task Escalation -' + '' + title,
                   // body: 'Hi,'+'<br><br>'msg,
                    body:'Hi'+' '+empName+','+'<br><br>'+msg+'<br><br>'+'Please open '+' '+link+' '+'to view the task record.'+'<br><br>'+'Thank You.'

                });
            } catch (e) {
                log.debug('Error@SendEmail', e)
            }
        }

        //Check parameter function
        function checkForParameter(parameter) {

            if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false' && parameter !== '- None -') {

                return true;

            }

        }

        return {afterSubmit}

    });

Leave a comment

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