Invoice Email

script that triggers only on Create context of Invoice,that will send the email
using the email template “CTG-Invoice” to anyone with “receive invoice” (on the customer
form, under Relationships) checkbox checked.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */


/*************************************************************
 * CTG-31
 * Author: Jobin & Jismi IT Services LLP
 *
 *************************************************************/

define(['N/record', 'N/search','N/runtime','N/email','N/render'],
    /**
 * @param{record} record
 * @param{search} search
 */
    (record, search, runtime,email, render) => {


        const afterSubmit = (scriptContext) => {

            try {
                if (scriptContext.type === scriptContext.UserEventType.CREATE) {
                    let currentRec = scriptContext.newRecord;

                    log.debug("record Id", currentRec);

                    let customerName = currentRec.getValue({
                        fieldId: 'entity'
                    })

                    log.debug("Customer Name", customerName);

                    let terms=currentRec.getValue({
                        fieldId:'terms'
                        })
                        log.debug("terms", terms)
                    let invoiceId = currentRec.id;
                    log.debug("invoiceId", invoiceId);

                    // load search
                    var customerSearch = search.load({
                        id: 'customsearch_jj_ctg_31_cust_search_inv'
                    });
                    var custArray = [];
                    var internalId;
                    var searchResultCount = customerSearch.runPaged().count;

                    customerSearch.run().each(function (result) {

                        internalId = result.getValue({name: "internalid", label: "Internal ID"})
                        custArray.push(internalId)
                        return true;
                    });
                    log.debug("custArray", custArray);

                    var checkExist =custArray.includes(customerName);
                    log.debug("checkExist", checkExist);

                    if(checkExist === true)

                     {

                    var contactSearchObj = search.create({
                        type: "contact",
                        filters:
                            [
                                ["company", "anyof", customerName],
                                "AND",
                                ["custentity_invoicenotification", "is", "T"],

                            ],

                        columns:
                            [
                                search.createColumn({
                                    name: "entityid",
                                    sort: search.Sort.ASC,
                                    label: "Name"
                                }),
                                search.createColumn({name: "email", label: "Email"}),

                                search.createColumn({name: "company", label: "Company"}),


                                search.createColumn({name: "internalid", label: "Internal ID"}),

                                search.createColumn({name: "custentity_invoicenotification", label: "Receive Invoices"})
                            ]
                    });

                    var searchResult = contactSearchObj.run();
                    log.debug("customerSearchObj result", searchResult);

                    var emailid;
                   // var internalId;
                    var results = searchResult.getRange({start: 0, end: 50});

                    for (var i = 0; i < results.length; i++) {

                        emailid = results[i].getValue({name: "email", label: "Email"});
                        log.debug("email", emailid);

                        // for (var j = 0; j < results.length; j++) {
                        //
                        //     internalId = results[j].getValue({name: "internalid", label: "Internal ID"});
                        //     log.debug("internalId", internalId);


                            var currentuser = runtime.getCurrentUser().id;
                            //Get Email Template
                            var mergeResult = render.mergeEmail({
                                templateId: 10,
                                transactionId: invoiceId,
                                entity: {
                                    type: 'customer',
                                    id:parseInt(customerName)
                                }

                            });
                            var emailSubject = mergeResult.subject;
                            var emailBody = mergeResult.body;

                            //Creating PDF object of the transaction
                            var transactionFile = render.transaction({
                                entityId: invoiceId,
                                printMode: render.PrintMode.PDF
                            });

                            if (emailid && terms ) {

                                email.send({
                                    author: currentuser,
                                    recipients: emailid,
                                    subject: emailSubject,
                                    body: emailBody,
                                    attachments: [transactionFile],
                                    relatedRecords:{
                                        transactionId:parseInt(invoiceId)
                                    }
                                });
                            }

                        }

                    }
                }

            }catch (e) {
                log.debug("error", e)
            }
        }

        return { afterSubmit}

    });

Leave a comment

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