Add Rich Text Editor & Customize Email Send Action in Task Record

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/************************************************************************************************
 * * EXPT-347 Customize the Email Sending Functionality**
 *
 * **********************************************************************************************
 *
 * Author: Jobin and Jismi IT Services
 *
 * Date Created : 21-June-2024
 *
 * Created By: JJ0124, Jobin and Jismi IT Services
 *
 * Description : Send Custom Email to the assignee when the field NOTIFY ASSIGNEE BY EMAIL is checked.
 *
 * REVISION HISTORY


 ***********************************************************************************************/
define([
    "N/ui/serverWidget",
    "N/email",
    "N/runtime",
    "N/record"
], (serverWidget, email, runtime, record) => {

    // Environment Constants
    const ENVIORNMENTS = {
        SANDBOX: "4071463-sb1",
        PRODUCTION: "4071463"
    }

    /**
     * Defines the function definition fetches the Field values.
     * @param {Object} newRecord 
     * @param {String} currentUser 
     * @returns Object
     */
    const retrieveEmailData = (newRecord, currentUser) => {
        try {
            let loadProejct = record.load({
                type: 'job',
                id: newRecord.getValue({ fieldId: "company" })
            });
            return {
                userName: currentUser.name,
                assigneeName: newRecord.getText({ fieldId: "assigned" }),
                companyName: "Expertec Van Systems Inc.",
                taskLink: `https://${ENVIORNMENTS[runtime.envType]}.app.netsuite.com/app/crm/calendar/task.nl?id=` + newRecord.id,
                taskName: newRecord.getValue({ fieldId: "title" }),
                priority: newRecord.getText({ fieldId: "priority" }),
                status: newRecord.getText({ fieldId: "status" }),
                startDate: newRecord.getText({ fieldId: "startdate" }),
                dueDate: newRecord.getText({ fieldId: "duedate" }),
                message: newRecord.getValue({ fieldId: "custevent_jj_email_body" }),
                projectId: loadProejct.getValue({ fieldId: "entityid" })
            }
        } catch (error) {
            log.error("Error in retrieveEmailData", error);
        }
    }

    /**
     * Defines the function definition that prepare the email content.
     * @param {Object} emailBodyValues 
     * @returns {string} body
     */
    const prepareEmailContent = (emailBodyValues) => {
        try {
            const {
                userName,
                companyName,
                taskLink,
                taskName,
                priority,
                status,
                startDate,
                dueDate,
                message,
                projectId
            } = emailBodyValues;

            let body = `
                <span>The following task has been assigned to you by ${userName} in ${companyName}</span><br><br>
                <span>Information regarding the task has been posted below.</span><br>
                <span>To view the task record, log in to NetSuite then navigate to: <a href="${taskLink}" target="_blank">${taskName}</a></span><br><br>
                <span><b>Task:</b> ${taskName}</span><br>
                <span><b>Priority:</b> ${priority}</span><br>
                <span><b>Status:</b> ${status}</span><br>
                <span><b>Start Date:</b> ${startDate}</span><br>
                <span><b>Due Date:</b> ${dueDate}</span><br>
                <span><b>Comments:</b> ${message}</span><br>
                <span><b>Associated companies, contacts:</b></span><br>
                <ul><li>${projectId}</li></ul>`;

            let htmlContent = `<html><title>Custom Email</title><body>${body}</body></html>`;

            return htmlContent;
        } catch (error) {
            log.error("Error in prepareEmailContent", error);
        }
    }

    /**
     * Defines the function definition that is executed before record is loaded.
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
     * @param {Form} scriptContext.form - Current form
     * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
     * @since 2015.2
     */
    const beforeLoad = (scriptContext) => {
        try {
            let { newRecord, form } = scriptContext;
            if (newRecord.type === "task") {
                form.getField({ id: "sendemail" }).updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN });
            }

            form.getField({ id: "message" }).updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN });
        } catch (error) {
            log.error("Error in beforeLoad", error);
        }
    }

    /**
     * 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 {
            let { newRecord } = scriptContext;
            if (newRecord.type !== "task") { return; }

            let notifyAssigneeByEmail = newRecord.getValue({ fieldId: "custevent_jj_notify_assignee_by_email" });
            log.error("notifyAssigneeByEmail", notifyAssigneeByEmail);
            if (!notifyAssigneeByEmail) { return };

            let currentUser = runtime.getCurrentUser();

            let emailBodyValues = retrieveEmailData(newRecord, currentUser);
            log.error("emailBodyValues", emailBodyValues);
            let emailContent = prepareEmailContent(emailBodyValues);
            log.error("emailContent", emailContent);

            email.send({
                author: currentUser.id,
                recipients: 'viyoosh.vinod@jobinandjismi.com', // This will be changed to newRecord.getValue({ fieldId: "assigned" }),  after testing
                subject: `${emailBodyValues.taskName}`,
                body: emailContent
            });

            record.submitFields({
                type: "task",
                id: newRecord.id,
                values: {
                    custevent_jj_notify_assignee_by_email: false
                },
            });
        } catch (error) {
            log.error("Error in afterSubmit", error);
        }
    }

    return { beforeLoad, afterSubmit }
});

Leave a comment

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