Merging Email Templates and Sending Approval Emails in NetSuite

Introduction:

Merging email templates with dynamic content and sending approval emails are key steps in automating approval workflows in NetSuite. This article covers the merging process and email sending functionality.

Key Script:

const sendEmail = {
        sendEmail(recordId, itemName) {
            try {
                const CUST_RECORD_TYPE_INVENTORY = "customrecord_grw007_invtitem";
                let script = runtime.getCurrentScript();
                let recipientEmail = script.getParameter({ name: 'custscript_grw_021_item_approv_recipient' });
                let emailTemplateId = script.getParameter({ name: 'custscript_grw_021_approval_email' });


                log.debug("recipientEmail", recipientEmail);
                log.debug("emailTemplateId", emailTemplateId);


                let extURL = url.resolveScript({ // getting External URL of the suitelet of Feedback
                    deploymentId: 'customdeploy_grw_021_sl_approve_item',
                    scriptId: 'customscript_grw_021_sl_approve_item',
                    returnExternalUrl: true
                });
                log.debug("extURL", extURL);


                let recordURL = url.resolveRecord({
                    recordType: CUST_RECORD_TYPE_INVENTORY,
                    recordId: recordId,
                    isEditMode: false
                });
        
                let approvalUrl = extURL + '&recordId=' + recordId + '&action=approve' + '&itemName=' + itemName;
                let rejectionUrl = extURL + '&recordId=' + recordId + '&action=reject' + '&itemName=' + itemName;


                log.debug("approvalUrl", approvalUrl);
                log.debug("rejectionUrl", rejectionUrl);


                let newEmailTempl = render.mergeEmail({
                    templateId: emailTemplateId,
                });
                let emailBody = newEmailTempl.body;
                let emailSubject = newEmailTempl.subject;


                emailBody = emailBody.replaceAll('{{approvalUrl}}', approvalUrl);
                emailBody = emailBody.replaceAll('{{rejectionUrl}}', rejectionUrl);
                emailBody = emailBody.replaceAll('{{itemName}}', itemName);
                emailBody = emailBody.replaceAll('{{itemId}}', recordId);
                emailBody = emailBody.replaceAll('{{recordRedirect}}', recordURL);


                emailSubject = emailSubject.replaceAll('{{itemName}}', itemName);
        
                email.send({
                    author: -5,
                    recipients: recipientEmail,
                    subject: emailSubject? emailSubject: "Approval Needed for Item '" + itemName + "' wit id " + recordId,
                    body: emailBody,
                    relatedRecords: {
                        customRecord: {
                            id: recordId,
                            recordType: CUST_RECORD_TYPE_INVENTORY
                        }
                    }
                });


                // Update the record status to approved
                record.submitFields({
                    type: CUST_RECORD_TYPE_INVENTORY,
                    id: recordId,
                    values: {
                        custrecord_grw007_first_approver_email: true
                    }
                });


                return { status: "SUCCESS", reason: "EMAIL_SENT", data: [] };
            } catch (e) {
                log.error({ title: "Error @grw_021_cm_functions @sendEmail @sendEmail", details: e });
                return { status: "ERROR", reason: "ERROR_IN_SENDING_EMAIL", data: [] };
            }            
        }
    };

Detailed Explanation:

  • Email Template Merging:
  • render.mergeEmail(): Merges the email template with dynamic content.
  • templateId: Specifies the email template ID.
  • Extracts emailBody and emailSubject from the merged template.
  • Content Replacement:
  • replaceAll: Replaces placeholders in the email body and subject with dynamic values (approvalUrl, rejectionUrl, itemName, recordId, and recordURL).
  • Sending Email:
  • email.send(): Sends the email with specified author, recipients, subject, body, and relatedRecords.
  • Updating Record:
  • record.submitFields(): Updates the inventory item record to indicate that the approval email has been sent (custrecord_grw007_first_approver_email set to true).

Leave a comment

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