Bill payment window emails

Send emails when bill payments are done throw the pay bill window.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*************************************************************
 
define(['N/currentRecord', 'N/email', 'N/record', 'N/recordContext', 'N/render', 'N/runtime', 'N/search'],
    /**
 * @param{currentRecord} currentRecord
 * @param{email} email
 * @param{record} record
 * @param{recordContext} recordContext
 * @param{render} render
 * @param{runtime} runtime
 * @param{search} search
 */
    (currentRecord, email, record, recordContext, render, runtime, search) => {

        const checkForParameter = function checkForParameter(parameter, parameterName) {
            try {
                if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false') {
                    return true;
                } else {
                    if (parameterName)
                        log.debug('Empty Value found', 'Empty Value for parameter ' + parameterName);
                    return false;
                }
            } catch (e) {
                log.debug("error@checkForParameter", e)
            }
        }


        /**
       * Defines the function definition that is executed after record is submitted.
       * @param {Object} scriptContext
       * @param {Record} scriptContext.newRecord - New record
       * @param {Record} scriptContext.oldRecord - Old record
       * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
       * @since 2015.2
       */

        const afterSubmit = (scriptContext) => {
            try {
                if (scriptContext.type === 'paybills') {

                    let currentRec = scriptContext.newRecord;
                    let billPayment = currentRec.id;
                    let donotEmail = currentRec.getValue({
                        fieldId: 'cseg_jj_donot_email'
                    })
                    let paymentAccount = currentRec.getValue({
                        fieldId: 'account'
                    })
                    //check if payment done through credit card
                    if ((paymentAccount == '978' || paymentAccount == '1106') && donotEmail == "2") {

                        let vendorId = currentRec.getValue({
                            fieldId: 'entity'
                        })
                        //get email ids
                        let customerEmails = search.lookupFields({
                            type: "vendor",
                            id: vendorId,
                            columns: ["custentity_2663_email_address_notif", "email"]
                        });
                        let eftNotificationEmail = customerEmails.custentity_2663_email_address_notif;
                        let primaryEmail = customerEmails.email;
                        let recipientEmail = [];
                        if (checkForParameter(eftNotificationEmail)) {
                            recipientEmail.push(eftNotificationEmail)
                        } else if (checkForParameter(primaryEmail)) {
                            recipientEmail.push(primaryEmail)
                        }
                        if (recipientEmail.length > 0) {
                            let transactionFile = render.transaction({
                                entityId: billPayment,
                                printMode: render.PrintMode.PDF
                            });
                            //send email to the vendor
                            email.send({
                                author: '43659',
                                recipients: recipientEmail,
                                subject: 'NOTIFICATION TO RUN PAYMENT ON CREDIT CARD',
                                body: '<div>Please run the attached payment through on the credit card you have on file for Pure Care Inc.<br />If you have any questions or need assistance regarding this payment, please call 1-587-773-3272.<br/> <br />Thank you,<br /><br /><b>PURE CARE INC.</b><br /><b>Accounts Payable Dept.</b></div>',
                                attachments: [transactionFile],
                                relatedRecords: {
                                    entityId: vendorId,
                                    transactionId: billPayment
                                }
                            });
                            record.submitFields({
                                type: record.Type.VENDOR_PAYMENT,
                                id: billPayment,
                                values: {
                                    'tranid': ''
                                }
                            });
                        }
                    }
                    else if ((paymentAccount == '1021' || paymentAccount == '1103' || paymentAccount == '1104' || paymentAccount == '1107') && donotEmail == "2") {

                        let vendorId = currentRec.getValue({
                            fieldId: 'entity'
                        })
                        //get email ids
                        let customerEmails = search.lookupFields({
                            type: "vendor",
                            id: vendorId,
                            columns: ["custentity_2663_email_address_notif", "email"]
                        });
                        let eftNotificationEmail = customerEmails.custentity_2663_email_address_notif;
                        let primaryEmail = customerEmails.email;
                        let recipientEmail = [];
                        if (checkForParameter(eftNotificationEmail)) {
                            recipientEmail.push(eftNotificationEmail)
                        } else if (checkForParameter(primaryEmail)) {
                            recipientEmail.push(primaryEmail)
                        }
                        if (recipientEmail.length > 0) {
                            let transactionFile = render.transaction({
                                entityId: billPayment,
                                printMode: render.PrintMode.PDF
                            });

                            let emailBody = '<div>Please be advised of the attached direct deposit payment made to your bank account today.<br /> If you have not received the funds into your bank account within 1-2 business days, please notify us immediately and we will put a tracer on the payment.<br />Please call 1-587-773-3272 if you have any questions or need assistance regarding this payment.<br /><br/>Thank you,<br /><br /><b>PURE CARE INC.</b><br /><b>Accounts Payable Dept.</b></div>'

                            //send email to the vendor
                            email.send({
                                author: '43659',
                                recipients: recipientEmail,
                                subject: 'DIRECT DEPOSIT REMITTANCE ADVICE',
                                body: emailBody,
                                attachments: [transactionFile],
                                relatedRecords: {
                                    entityId: vendorId,
                                    transactionId: billPayment
                                }
                            });
                            record.submitFields({
                                type: record.Type.VENDOR_PAYMENT,
                                id: billPayment,
                                values: {
                                    'tranid': ''
                                }
                            });
                        }
                    }
                }

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

        return { afterSubmit }

    });

Leave a comment

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