Sending Email NCPS-65

Description

When a user submits the form after filling the details, the corresponding custom record will be created in the NetSuite. Then the created form will be sent to the vendor. In order to send the form to the vendor, we will be doing a customization in the custom record.
We will be implementing a checkbox ”To be emailed” in the custom record.

  • If the “To be emailed” checkbox is checked, a user event script will send an email to the vendor with the contractor health and safety agreement form after the project manager submitting the custom record.

Solution

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

/************************************************************************************************
 NCPS - 65 Script for Sending Email
 *********************************************************************************************
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 * Date Created : 06-January-2022
 *
 * Description :  Send Email to the vendor when creating a custom record.
 *
 * REVISION HISTORY
 *
 ***********************************************************************************************/
define(['N/currentRecord', 'N/email', 'N/file', 'N/record', 'N/runtime', 'N/search', 'N/render'],
    /**
     * @param{currentRecord} currentRecord
     * @param{email} email
     * @param{file} file
     * @param{record} record
     * @param{render} render
     * @param{search} search
     */
    (currentRecord, email, file, record, runtime, search, render) => {
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */

        const afterSubmit = (scriptContext) => {

            try {

                // Email sends only while create a new custom record

                if (scriptContext.type == 'create') {
                    let currentRecord = scriptContext.newRecord;
                    let currentVendorId = currentRecord.getValue({
                        fieldId: 'custrecord_jj_cr_vendor_ncps62'
                    });

                    let currentValue = currentRecord.getValue({fieldId: 'custrecord_jj_cr_to_be_emailed_ncps62'});
                    if (currentValue == true) {
                        log.debug("test",currentValue)
                        var objRecord = record.load({
                            type: "customrecord_jj_cr_safety_form_ncps62",
                            id: currentRecord.id
                        })

                        // For rendering the PDF
                        let myFile = render.create();
                        let templateId = 104;

                        myFile.setTemplateById(templateId);

                        myFile.addRecord({
                            templateName: 'record',
                            record: objRecord
                        });


                        let vgmFile = myFile.renderAsPdf();

                        let pdfName = "Contractor Health and Safety Agreement Form"

                        vgmFile.name = pdfName;

                         //To get the runtime user
                        let userObj = runtime.getCurrentUser();
                        let runtimeUser = userObj.id;

                        email.send({
                            author: runtimeUser,
                            recipients:currentVendorId,
                            subject: 'Contractor Health and Safety Agreement Form',
                            body: 'Hi, \n \n Attaching the Contractor health and safety agreement form \n \n ThankYou!',
                            attachments: [vgmFile]

                        });


                    }
                }


            } catch (err) {

                log.debug('error', err);

            }
        }

        return {afterSubmit}

    });

Leave a comment

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