How to attach email to transaction record?

Hi All,

In this post we are going to learn about " Attach email to record", In our example we will attach email to transaction record , you can attach to record type you want

We need "N/email" module to send email, use "email.send()" method to send email. 

Author, body, recipients and subject are required parameters where as attachments, bcc, cc, isInternalOnly, relatedRecords, and replyTo are optional fields. 

Mandatory FieldsOptional Fields
authorattachments
bodybcc
recepientscc
subjectisInternalOnly

relatedRecords

replyTo

 We can also attach to :

1. Case and Campaign use "activityId" 

2.  Custom Record use "customRecord" pass object like below:

        customRecord:{
          id:recordId,
          recordType: recordTypeId
        }  

3. Entity record like employee customer etc use "entityId"

4. Transaction record use "transactionId"


Working Code:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */

define(['N/record','N/email', 'N/render'],

    function(record, email, render) {

        function attachToTrans(context) {

            try
            {
                var newrecord = context.newRecord;
               
                var recordid = newrecord.id;               
                var recordtype = newrecord.type;
                //====== you can print pdf and attach to it ====
                var transactionFile = render.transaction({
                                    entityId: recordid,
                                    printMode: render.PrintMode.PDF,
                                    inCustLocale: true
                                    });
                //=================End of Print PDF ========================
               
                //============ You can use email template also ============
               
                var ngMergeResult = render.mergeEmail({
                                            templateId: XXYY, //XXYY is email template id
                                            transactionId: recordid,
                                            });
                var emailSubject = ngMergeResult.subject;
                var emailBody = ngMergeResult.body;
                       
                //=========== end of email template ========================

                //================ send email and attach to transaction record======
                email.send({
                            author: AABBCC,//AABBCC is author id
                            recipients: myEmailList, //myEmailList array of all emails or one email
                            subject: emailSubject,
                            body: emailBody,
                            cc: myCCs,//myCCs array of emails or email
                            attachments: [transactionFile],
                            relatedRecords: {
                                transactionId: recordid
                            }
                        });
                //========== end of sending email ========================
            }
            catch(exp)
            {
                log.error({
                            title: 'exp ',
                            details: "exp "+exp
                            });
            }
        }

        return {
            afterSubmit: attachToTrans

        };
});

 

 Hope this will help someone. Please let us know in comment section below or follow/ join us on FB and linkedin


Thanks

Netsuite Guru

 

Download Netsuite Guru Android App
Follow Me on Linkedin
Our FB Page

Comments