Script to send an automated email once the customer submits the return online

Requirement

Set up an automated email system to send customers a confirmation email once they submit a return request online. This email will provide important information about the return process, including details about the Return Merchandise Authorization (RMA) and the return label.

Create an automated email once the customer submits the return online of what to expect and time-frame for the RMA and label to be received.

Solution

Step 1

Create a Email Template from Documents > Templates > Email Templates

Step 2

Deploy a User event script in the Return Authorization record with the following code.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*************************************************************************************************************************
 * Toolfetch LLC-USA-NS
 * 
 * ${TFLL-299} : ${Create an automated email once the customer submits the return online of what to expect and time-frame for the RMA and label to be received.}
 * 
 *********************************************************************************************************************
 *
 * Author: Jobin & Jismi IT Services
 * 
 * Date Created : 31-August-2023
 * 
 * Description :This Userevent script is used to send an automated email to the customer when a return has been placed through the online form.
 *
 * REVISION HISTORY
 *
 * @version 1.0 TFLL-299 : 31-August-2023 : Created the initial build by JJ0170
 * 
 **************************************************************************************************************************/
define(['N/email', 'N/record', 'N/search', 'N/render'],
    /**
     * @param {email} email 
     * @param {record} record 
     * @param {search } search 
     * @param {render } render 
     * @returns 
     */
    (email, record, search, render) => {
        /**
         * Function to check whether the RMA is created by submitting the online returns form in Website
         * @param {number} rmaInternalId - Internal id of the RMA
         * @returns {boolean} true/false
         */
        function returnAuthorizationSearch(rmaInternalId) {
            try {
                let returnAuthorizationSearchObj = search.create({
                    type: "returnauthorization",
                    filters:
                        [
                            ["type", "anyof", "RtnAuth"],
                            "AND",
                            ["internalid", "anyof", rmaInternalId],
                            "AND",
                            ["systemnotes.type", "is", "T"],
                            "AND",
                            ["systemnotes.context", "anyof", "WST"],
                            "AND",
                            ["mainline", "is", "T"]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });
                let searchResultCount = returnAuthorizationSearchObj.runPaged().count;
                if (searchResultCount > 0) {
                    return true;
                }
                else {
                    return false;
                }
            }
            catch (e) {
                log.error("Error @ returnAuthorizationSearch", e);
                return false;
            }
        }

        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
            try {
                if (scriptContext.type === 'create') {
                    let rmaRecord = scriptContext.newRecord;
                    let rmaInternalId = rmaRecord.id;
                    //to check whether the RMA is created through the online form
                    let isCreatedFromWebsite = returnAuthorizationSearch(rmaInternalId);
                    if (isCreatedFromWebsite == true) {
                        //Getting Customer ID
                        let customerInternalId = rmaRecord.getValue({ fieldId: 'entity' });
                        //Lookup the customer to find email ID
                        let custEmail = search.lookupFields({
                            type: "customer",
                            id: Number(customerInternalId),
                            columns: ["email"]
                        }).email;
                        //Loading the email template
                        let newEmailTempl = render.mergeEmail({
                            templateId: 207,
                            transactionId: Number(rmaInternalId),
                            entity: { type: 'customer', id: Number(customerInternalId) }
                        });
                        let emailSubject = newEmailTempl.subject;
                        let emailBody = newEmailTempl.body;
                        email.send({
                            author: -5,
                            recipients: custEmail,
                            subject: emailSubject,
                            body: emailBody,
                            relatedRecords: { transactionId: rmaInternalId }
                        });
                    }
                }
            }
            catch (e) {
                log.error("Error @ afterSubmit", e);
            }
        }
        return { afterSubmit }
    });

Important

In order to trigger the script while submits a return online, we need to enable the “Webstore” context under the sub tab “Context Filtering” in the script deployment.

Output

Leave a comment

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