Aleksandar Đokić
Aleksandar Đokić

Reputation: 2343

Email template netsuite

I have really big problem, i mean im working in a company where we are using NetSuite as our business platform and now they updated our account with new APIs.

Developer before me wrote this code

for(var k = 0; k < ResultsPPCI.length; k++){

    EmployeePPCI = ResultsPPCI[k].getValue('internalid'); //get the value
    // log('Internal ID', EmployeePPCI);

    // Merge, send, and associate an email with Purchase Order record (id=1000)
    var mergeValuesPPCI = {};
    mergeValuesPPCI.NLEMPFIRST = nlapiLookupField('employee', EmployeePPCI, 'firstname');
    mergeValuesPPCI.NLEMPLAST = nlapiLookupField('employee', EmployeePPCI, 'lastname');
    mergeValuesPPCI.NLSUPPLIER = nlapiLookupField('customer', cust, 'companyname');
    mergeValuesPPCI.NLPRODUCTCODE = productcodehtml;

    var emailBodyPPCI = nlapiMergeRecord(65, 'purchaseorder', poID, null, null, mergeValuesPPCI);
    var recordsPPCI = {};
    recordsPPCI['transaction'] = poID;

    nlapiSendEmail(EmployeePPCI, EmployeePPCI, emailBodyPPCI.getName(), emailBodyPPCI.getValue(), null, null, recordsPPCI);
    // log('EmployeePPCI',EmployeePPCI);
    nlapiLogExecution('AUDIT', 'Potentional Problem', 'Email Sent');
}

I have problem now because nlapiMergeRecord is deprecated and it wont work. But i really cant find any working example online for hours... The most important part here is actually body of this email that has to be sent. In this case it is productcodehtml or mergeValuesPPCI.NLPRODUCTCODE.

This is how my template looks like :

<p>The QA Release has been submitted by <nlsupplier> for ${transaction.tranId}.</nlsupplier></p>

<p>The following item(s) have a short shelf life:</p>
<nlproductcode></nlproductcode>

Please can you help me with converting this code to new method? How can i connect nlproductcode from template with mergeValuesPPCI.NLPRODUCTCOD from my code?

Thanks in advance!

Upvotes: 1

Views: 2152

Answers (2)

Maxi
Maxi

Reputation: 173

You can use the new nlapiCreateEmailMerger(templateId)

First you need to create your email template in netsuite and get the internal id. Then:

  1. Use nlapiCreateEmailMerger(templateId) to create an nlobjEmailMerger object.

    var emailMerger = nlapiCreateEmailMerger(templateId);

  2. Use the nlobjEmailMerger.merge() method to perform the mail merge.

    var mergeResult = emailMerger.merge();

  3. Use the nlobjMergeResult methods to obtain the e-mail distribution’s subject and body in string format.

    var emailBody = mergeResult.getBody();

  4. Send your email

    nlapiSendEmail(senderInternalId, '[email protected]','subject',emailBody, null, null, null);

Good luck!

Upvotes: 1

bknights
bknights

Reputation: 15402

You can use kotnMergeTemplate as a drop in replacement for nlapiMergeRecord

e.g.:

kotnMergeTemplate(65, 'purchaseorder', poID, null, null, mergeValuesPPCI);

the nlobjEmailMerger does not take custom values so you'd have to post process the results. Again you can look at the example in my script where you'd get the merged string and then run:

        var oldCustFieldPatt = /<(nl[^ >]+)>(\s*<\/\1>)?/ig;
        content = content.replace(oldCustFieldPatt, function(a,m){
            return mergeValuesPPCI[m.toUpperCase()] || mergeValuesPPCI[m.toLowerCase()] || '';
        });

Upvotes: 1

Related Questions