How can send email using Email Template

To begin, we will create an Email Template in NetSuite using navigation Documents>Templates>Email Templates>new

Give name to the template and select the Recor Type from the drop-down list. In Template sub tab, select the TEXT EDITOR and write the content in the body.

Email Template – https://668649-sb1.app.netsuite.com/app/crm/common/merge/emailtemplate.nl?id=398

When the status is changed from active to closed in the Support case record, a user event script is used to send an email to the ineligible users

User Event Script – https://668649-sb1.app.netsuite.com/app/common/scripting/script.nl?id=403

/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
define(['N/email', 'N/record','N/render'], function (email, record,render) {

    function afterSubmit(context) {
        let oldRecord = context.oldRecord;
        let newRecord = context.newRecord;
        let oldDeclineReason = oldRecord.getValue({
            fieldId: 'custevent_approve_decline_reason'
        });
        let newDeclineReason = newRecord.getValue({
            fieldId: 'custevent_approve_decline_reason'
        });
      if (!oldDeclineReason && newDeclineReason) {
            log.debug('declining');
            //Set fields and send email
            let rec = record.load({
                type: record.Type.SUPPORT_CASE,
                id: newRecord.id,
                isDynamic: true
            });
            let caseId = newRecord.id;
           log.debug("caseId",caseId);
            rec.setValue({
                fieldId: 'status',
                value: 5
            });
            rec.setValue({
                fieldId: 'custevent_application_status',
                value: 3
            });
            rec.setValue({
                fieldId: 'custevent8',
                value: new Date()
            });
            let id = rec.save();
            let assigned = newRecord.getValue({
                fieldId: 'assigned'
            }) || newRecord.getValue({
                fieldId: 'custevent1'
            });
            let applicant = newRecord.getValue({
                fieldId: 'company'
            });
            let caseNumber = newRecord.getValue({
                fieldId: 'casenumber'
            });
            let currentDate = newRecord.getText({
                fieldId: 'lastmessagedate'
            });
           log.debug("currentDate",currentDate)
           var newDate = new Date();
           log.debug("newDate",newDate);
            var date1 = generateDateString(newDate)
            log.debug("date1",date1)
           var mergeResult = render.mergeEmail({
              templateId: 398,
              supportCaseId: caseId
             })
          var emailBody = mergeResult.body;
		  log.debug("emailBody",emailBody)
           var emailResult = emailBody.replace("currentdate",date1)
            let declineDetail = newRecord.getValue({
                fieldId: 'custevent_approve_decline_detail'
            });
            if (!applicant) {
                log.error('Cannot send email for case internalid: ' + newRecord.id);
            } else {
                email.send({
                    author: 174220,
                    recipients: applicant,
                    subject: 'Union Privilege Case# ' + caseNumber,
                    body: emailResult,
                   relatedRecords: {
                        activityId: newRecord.id,
                    }
                });
            }

        }
    }
  function generateDateString(dateObj) {
                                               //convert date to specific format
           var date = dateObj;
           var d = date.getDate();
           var m = date.getMonth() + 1;
           var y = date.getFullYear()
           var dateString = ((d <= 9 ? '0' + d : d) + '/' + (m <= 9 ? '0' + m : m) + '/' + y);
       
          return dateString;
    }

return {
           afterSubmit: afterSubmit
    }
});

Leave a comment

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