Email template from the item record.

Jira Code: NPL-5
A Suitelet is created to make an email template from an existing advanced template, then add the file to the item record. A button is created using workflow to generate the file again if any changes done to the item record.

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
/**

* Script Description

* This is a test suitlet to check the render method

*/

/*******************************************************************************
 * 
 * 
 * NetSuite Name :NPL-5 SL Create HTML
 * Script ID: 
 * 
 * *****************************************************************************
 * 
 *  
 * $Author: Jobin & Jismi IT Services LLP $
 * 
 * DESCRIPTION
 * This is a test suitlet to check the render method
 * 
 * Date Created :27-04-2019
 * 
 * REVISION HISTORY Update:
 * RA
 * 
 * 
 ******************************************************************************/
define(['N/search', 'N/file', 'N/render', 'N/record'],
    function(search, file, render, record) {
        var main = {
            onRequest: function(context) {
                var renderer = render.create();
                renderer.setTemplateById(126);

                var myContent = renderer.addRecord({
                    templateName: 'record',
                    record: record.load({
                        type: "customrecord_jj_offer_emailtemplate",
                        id: 2
                    })
                });

                var xml = renderer.renderAsString();
                log.debug('xml',xml);
                context.response.write(xml)
            }
        }
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };
        return main;
    });
/**
 * @NApiVersion 2.x
 * @NScriptType workflowactionscript
 * @NModuleScope SameAccount
 */
/**

* Script Description

* Workflow script to create a HTML file and add the id to custom field in the record

*/

/*******************************************************************************
 * 
 * 
 * NetSuite Name :NPL-5 WF JJ Create HTML in ITEM
 * Script ID: 
 * 
 * *****************************************************************************
 * 
 *  
 * $Author: Jobin & Jismi IT Services LLP $
 * 
 * DESCRIPTION
 * Workflow script to create a HTML file and add the id to custom field in the item record
 * 
 * Date Created :27-04-2019
 * 
 * REVISION HISTORY Update:
 * RA
 * 
 * 
 ******************************************************************************/
define(['N/search', 'N/file', 'N/render', 'N/record'],
    function(search, file, render, record) {
        var main = {
            onAction: function(context) {
                var renderer = render.create();
                renderer.setTemplateById(127);

                var Record = context.newRecord;
                var SetImage = main.setImage(Record);
                var setTaste = main.setTasteField(Record.id,Record);
                var myContent = renderer.addRecord({
                    templateName: 'record',
                    record: record.load({
                        type: record.Type.INVENTORY_ITEM,
                        id: Record.id
                    })
                });

                var html = renderer.renderAsString();

                var fileObj = file.create({
                    name: Record.id + ' ' + Date.now() + ' ' + 'Email Template.html',
                    fileType: file.Type.HTMLDOC,
                    contents: html,
                    folder: 64226
                });

                var fileID = fileObj.save();

                Record.setValue({
                    fieldId: 'custitem_email_template',
                    value: fileID
                });


            },
            setImage: function(Record) {
                var IMAGE_URL = "https://www.nickollsandperks.co.uk/site/Product%20Images/DISPLAY_NAME_1.jpg?resizeid=5&resizeh=1200&resizew=1200";
                var displayname = Record.getValue({ fieldId: 'displayname' });
                var itemDisplayUrl = IMAGE_URL.replace("DISPLAY_NAME", displayname.replace(/ /g, "%20"));

                Record.setValue({
                    fieldId: 'custitem_product_image',
                    value: itemDisplayUrl
                });
                return true;

            },
            setTasteField: function(ID,Record) {
                var tastingNotes = "";
                var customrecord_tastenotesSearchObj = search.create({
                    type: 'customrecord_tastenotes',
                    filters: [
                        ["custrecord_reviewitem", "anyof", ID]
                    ],
                    columns: [
                        search.createColumn({ name: "name", join: "CUSTRECORD_CRITIC", label: "Name" }),
                        search.createColumn({ name: "custrecord_rating", label: "Rating" }),
                        search.createColumn({ name: "custrecord_notes", label: "Notes" })
                    ]
                });

                customrecord_tastenotesSearchObj.run().each(function(result) {
                    tastingNotes += result.getValue(customrecord_tastenotesSearchObj.columns[0]) + "<BR>";
                    tastingNotes += result.getValue(customrecord_tastenotesSearchObj.columns[1]) + "<BR>";
                    tastingNotes += result.getValue(customrecord_tastenotesSearchObj.columns[2]) + "<BR><BR><BR>";
                    return true;
                });

                 Record.setValue({
                    fieldId: 'custitem_tasting_notes',
                    value: tastingNotes
                });
                return true;
            }


        }
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };
        return main;
    });

Leave a comment

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