2

When sending an email using nlapiSendEmail() can I specify a email template to use?

I have created an email template in the NetSuite backend. Is there a function I can use to send an email and use that email template?

sazr
  • 24,984
  • 66
  • 194
  • 362

2 Answers2

2

You can try using nlapiCreateEmailMerger(templateId) to get the body and subject of the email:

var emailMerger = nlapiCreateEmailMerger(templateId);

var mergeResult = emailMerger.merge();
var body = mergeResult.getBody();
var subject = mergeResult.getSubject();

nlapiSendEmail(author, recipient, subject, body, null, null, null, null);
Andrés Andrade
  • 2,213
  • 2
  • 18
  • 23
2

I do mine like this:

var emailSendID='xxxx'; // Email author ID
var emailTempID=123;    // Template ID
var emailTemp=nlapiLoadRecord('emailtemplate',emailTempID); 
var emailSubj=emailTemp.getFieldValue('subject');
var emailBody=emailTemp.getFieldValue('content');

var renderer=nlapiCreateTemplateRenderer();
renderer.setTemplate(emailSubj);
renderSubj=renderer.renderToString();
renderer.setTemplate(emailBody);
renderBody=renderer.renderToString();

nlapiSendEmail(emailSendID,'noreply@xxxxx',renderSubj,renderBody,finalEmailArray,bccEmailArray);
w3bguy
  • 2,215
  • 1
  • 19
  • 34
  • 1
    thanks this really helps. How can I add custom text to the template? For example; the template is for 'Contact us' so theres the general template/look of the email then I need to insert the contact us details such as the persons name, phone and message. Is there a way to insert that into the template body (`emailTemp.getFieldValue('content')`)? – sazr Jul 16 '17 at 23:37
  • I have not tried that. Though, you can create variables in the template, that pull from the records. You may be able to use those to link data into the template. – w3bguy Jul 17 '17 at 20:20