Attaching Customized Template for Emails

Clients: Manglam Electricals, CPH Group

Advanced PDF Templates customized for custom Button prints won’t be available to attach in Automated Emails. So we add a custom Email Button in Record and render the PDF in Button click and attach it to the Email.

Client

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:CPH Group Australia
 * CGA-29
 *  High quality quote templates (up to 4 pages) with 2 or 3 options. 
 * **************************************************************************
 * Date : 09-07-2019
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : This script is to to define the action of print button and Mail Button
 * Date created : 09-07-2019
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${09-07-2019} nd :marg created
 * 
 *
 ******************************************************************************/
define(['N/currentRecord', 'N/url', 'N/https', 'N/search'],

    function(currentRecord, url, https, search) {


        function pageInit(scriptContext) {



        }



        function printbuttonClick() {
            try {
                var record = currentRecord.get();
                var recId = record.id;
                var recType = record.type;

                get_url = url.resolveScript({
                    scriptId: "customscript_jj_cga_29_quote_print",
                    deploymentId: "customdeploy_jj_cga_29_quote_print"
                });

                get_url += ('&recId=' + recId + '&recType=' + recType);
                window.open(get_url);

            } catch (er) {
                console.log('error at printbuttonClick', er)
            }
        }

        function mailButtonPrint() {
            try {

                var record = currentRecord.get();
                var recId = record.id;
                var recType = record.type;


                console.log('recId', recId)
                console.log('empid', empid)
                console.log('record', record)
                console.log('recType', recType)



                var empid = search.lookupFields({
                    type: recType,
                    id: recId,
                    columns: ['entity']
                });

                empid = empid.entity[0].value;



                var isCustom = true;




                var get_url = "https://3575471.app.netsuite.com/app/crm/common/crmmessage.nl?transaction=" + recId + "&entity=" + empid + "&l=T&templatetype=EMAIL";

                get_url += ('&isCustom=' + isCustom + '&recId=' + recId + '&recType=' + recType);
                window.open(get_url);



            } catch (er) {
                console.log('error at mailButtonPrint', er)
            }
        }


        return {

            pageInit: pageInit,
            printbuttonClick: printbuttonClick,
            mailButtonPrint: mailButtonPrint
        };

    });

User Event-Adding Button

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
* CLIENTNAME:CPH Group Australia
* CGA-29
*  High quality quote templates (up to 4 pages) with 2 or 3 options. 
* **************************************************************************
* Date : 09-07-2019
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : This script is to to add button in Quote 
* Date created : 09-07-2019
*
* REVISION HISTORY
*
* Revision 1.0 ${09-07-2019} nd :marg created
* 
*
******************************************************************************/
define([],

function() {
   
    
    function beforeLoad(scriptContext) {

    	try{
    		var propRec =  scriptContext.form;
    	  
        	 if(scriptContext.type == 'view'){
        		propRec.clientScriptFileId = 2155341;
        		 var button = propRec.addButton({
             		id: 'custpage_printproposal',
             		label :'Print Quote',
             		functionName: 'printbuttonClick'
				  });
				  var printbutton = propRec.addButton({
					id: 'custpage_printmail',
					label :'Send Email',
					 functionName: 'mailButtonPrint'
				 });
     		}
    	}catch(err){
    		log.debug('error @ beforeLoad',err)
    	}
    	

    

    }

   

    return {
        beforeLoad: beforeLoad
    };
    
});

User Event -Message Record

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:CPH Group Australia
 * CGA-29
 *  High quality quote templates (up to 4 pages) with 2 or 3 options. 
 * **************************************************************************
 * Date : 16-07-2019
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : This script for setting the recipient and message in email message 
 * Date created : 16-07-2019
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${16-07-2019} nd :marg created
 * Revision 1.1 ${1-08-2019} nd :marg Updation in the Prbound and CNC 
 *                               proposal templates 
 * 
 *
 ******************************************************************************/
define(['N/email', 'N/record', 'N/search', 'N/url', 'N/https', 'N/file', 'N/render'],

    function(email, record, search, url, https, file, render) {

        function beforeLoad(scriptContext) {



            try {

                var isCustom = scriptContext.request.parameters.isCustom;
                var recId = scriptContext.request.parameters.recId;
                var recType = scriptContext.request.parameters.recType


                log.debug('recType', recType)

                if (isCustom) {





                    var objRecord = scriptContext.newRecord;


                    var proposal = record.load({
                        type: recType,
                        id: recId
                    });

                    log.debug('proposal', proposal)


                    var division = proposal.getValue({

                        fieldId: 'custbody_division'

                    });


                    var cncItem = findCNCItem(recId);

                    var myFile = render.create();


                    if (division == "CPH GROUP" && cncItem != 0) {

                        var template = 104;

                    } else if (division == "CPH GROUP" && cncItem == 0) {


                        var template = 103;


                    } else {


                        var template = 105;




                    }


                    var dd = myFile.setTemplateById(template);


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





                    var transactionFile = myFile.renderAsPdf();

                    transactionFile.folder = 1027764;
                    transactionFile.name = 'Proposal';
                    var fileId = transactionFile.save();

                    log.debug('transactionFile', transactionFile)









                    //Attaching the prposal  file in the email
                    objRecord.setSublistValue({
                        sublistId: 'mediaitem',
                        fieldId: 'mediaitem',
                        value: fileId,
                        line: 0,
                        ignoreFieldChange: true
                    });

                    objRecord.save();



                }







            } catch (e) {
                log.debug("Err@ beforeLoad FN ", e);

            }


        }



        return {
            beforeLoad: beforeLoad

        };


        function findCNCItem(id) {


            var cncSearch = search.create({
                type: "estimate",
                filters: [
                    ["type", "anyof", "Estimate"],
                    "AND", ["custbody_division", "startswith", "CPH"],
                    "AND", ["item.name", "startswith", "CNC"],
                    "AND", ["internalid", "anyof", id]
                ],
                columns: [
                    search.createColumn({
                        name: "internalid",
                        summary: "COUNT",
                        label: "Internal ID"
                    })
                ]
            });

            var count;

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

                count = result.getValue({

                    name: "internalid",
                    summary: "COUNT",
                    label: "Internal ID"

                });

                return true;
            });

            return count;


        }



    });

Leave a comment

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