Send PO in Excel Format

Jira code: TEL-2 Task 1

Created a Purchase order in excel format and mail it on a button click from the purchase order record. For that place one button in the PO record. On that button click, I have created the purchase order as an Excel file and it’s mailed with a specific sender, recipient and a message body.

User Event Script

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

/**
 * Script Description: This script is for setting a button 'Send Purchase Order' in the Purchase Order record's Edit/View mode
 */

/*******************************************************************************
 * * Telegram* *
 * **************************************************************************
 * Date:9/2/18 
 * Script name: TG UE Email Btn PO
 * Script id: customscript_tg_ue_email_btn_po
 * Deployment id: customdeploy_tg_ue_email_btn_po
 * Applied to: Purchase Order Record
 * 

 ******************************************************************************/

define(['N/record'],

function(record) {
   
	
	/**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {string} scriptContext.type - Trigger type
     * @param {Form} scriptContext.form - Current form
     * @Since 2015.2
     */
    function beforeLoad(scriptContext) {
    	try {
    		
    		//Set a button and Provide the action for the button by calling the function in client script
			var CustRecPO=scriptContext.form;	
			CustRecPO.addButton({
			id:'custpage_newemail_btn',
			label:'Send Purchase Order',
			functionName:'createEmailAction_PO'
			
			});
			
			//Loading Client Script
			CustRecPO.clientScriptModulePath = 'SuiteScripts/TG CS Email Btn Action PO.js';
			
			
		} catch (e) {
			log.debug({    
				title: e.name,
                details: e.message
            });
		}

    }

    return {
        beforeLoad: beforeLoad
    };
    
});

Client Script

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

/**
 * Script Description: This script defining the function of 'Send Purchase Order' button action
 * Using this script redirect to suitelet   
 */

/*******************************************************************************
 * * Telegram* *
 * **************************************************************************
 * Date:9/2/18 
 * Script name: TG CS Email Btn Action PO
 * Script id: customscript_tg_cs_email_btn_action_po
 * Deployment id: customdeploy_tg_cs_email_btn_action_po
 * Applied to: Purchase Order Record
 * 

 ******************************************************************************/

define(['N/url','N/currentRecord','N/https'],

function(url,currentRecord,https) {
    
    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     */
    function pageInit(scriptContext) {
    	
    }
    

    return {
        pageInit: pageInit,
        createEmailAction_PO:  function(){
        	
        	//Getting current record id
			var currentRec = currentRecord.get();
			var id=currentRec.id;
			
			//Setting the url of the suitelet script 
	    	var output = url.resolveScript({
			    scriptId: 'customscript_tg_ss_excel_po',
			    deploymentId: 'customdeploy_tg_ss_excel_po',
			    returnExternalUrl: false,
			})+ '&recId=' + id;
			
	    	//Opens url on a new Window
			newWindow = window.open(output, 'netsuite', 'menubar=1,resizable=1,width=740,height=800');
		}
    };
    
});

Suitelet

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
define([ 'N/record', 'N/search', 'N/ui/serverWidget', 'N/render', 'N/xml',
		'N/file', 'N/encode', 'N/redirect', 'N/runtime'],

function(record, search, serverWidget, render, xml, file, encode, redirect, runtime) {

	/**
	 * Definition of the Suitelet script trigger point.
	 * 
	 * @param {Object}
	 *            context
	 * @param {ServerRequest}
	 *            context.request - Encapsulation of the incoming request
	 * @param {ServerResponse}
	 *            context.response - Encapsulation of the Suitelet response
	 * @Since 2015.2
	 */
	function onRequest(context) {
		try {
			
			//Getting the record id
			var recordid = context.request.parameters.recId;
			log.debug({
				title : "poId",
				details : recordid
			});
			
			//Load the record 
			var poRecord = record.load({
				type : record.Type.PURCHASE_ORDER,
				id : recordid,
				isDynamic : true
			});
			
			//Getting the required details from the loaded record
			var po_number=poRecord.getValue({
				fieldId : 'tranid'
			});
			
			var entity=escape_for_xml(poRecord.getText({
				fieldId : 'entity'
			}),xml);
			
			var freight=poRecord.getValue({
				fieldId : 'custbody_tc_freight_providor'
			});
			var order_date=poRecord.getText({
				fieldId : 'trandate'
			});
			
			//var order_date = order_date1.split('T');
			
			log.debug({
				title : "order_date",
				details : order_date
			});
			
			var incoTerm=poRecord.getText({
				fieldId : 'incoterm'
			});
			var delMethod=poRecord.getValue({
				fieldId : 'custbody_tc_delivery_method_inwards'
			});
			
			var currency_type=poRecord.getText({
				fieldId : 'currency'
			});
		
			
			var count = poRecord.getLineCount({
				sublistId : 'item'
			});
			
			var itemSubTotal = 0.0;
			var itemArray=new Array;
			
			
			//Getting the item details and insert into a Array
			for (var i = 0; i < count; i++) {
				var item=new Object;
				 item.itemCode = escape_for_xml(poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'item_display',
					line : i
				}),xml);
				log.debug({
					title : "itemCode",
					details : item.itemCode
				});

				 item.vendorCode = escape_for_xml(poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'vendorname',
					line : i
				}),xml);
				log.debug({
					title : "vendorCode",
					details : item.vendorCode
				});

				 item.itemName = escape_for_xml(poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'custcol_item_name',
					line : i
				}),xml);
				log.debug({
					title : "itemName",
					details : item.itemName
				});
				
				item.item = escape_for_xml(poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'item',
					line : i
				}),xml);
				log.debug({
					title : "item",
					details : item.item
				});
				
				 item.itemDesc = escape_for_xml(poRecord.getSublistValue({
						sublistId : 'item',
						fieldId : 'description',
						line : i
					}),xml);
					log.debug({
						title : "itemDesc",
						details : item.itemDesc
					});

				 item.upcCode = poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'custcol_upc_code',
					line : i
				});
				log.debug({
					title : "upcCode",
					details : item.upcCode
				});

				 item.quantity = poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'quantity',
					line : i
				});
				log.debug({
					title : "quantity",
					details : item.quantity
				});

				 item.rate = poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'rate',
					line : i
				});
				log.debug({
					title : "rate",
					details : item.rate
				});

				item.amount = parseFloat(poRecord.getSublistValue({
					sublistId : 'item',
					fieldId : 'amount',
					line : i
				}));
				log.debug({
					title : "amount",
					details : item.amount
				});
				
				itemArray.push(item);
				itemSubTotal = itemSubTotal + item.amount;

			}

			var total = poRecord.getValue({
				fieldId : 'total'
			});
			
			log.debug({
				title : "itemArray",
				details : itemArray
			});
			

			var address = poRecord.getValue({
				fieldId : 'billingaddress_text'
			});
			var replacedAddress = address.replace(/\r?\n|\r/g,
			'\\');
			var addressArray = replacedAddress.split("\\");
			log.debug({
				title : "addressArray",
				details : addressArray
			});
			log.debug({
				title : "name",
				details : addressArray[0]
			});
			var name=addressArray[0];

			
			var emailId = poRecord.getValue({
				fieldId : 'email'
			});
			
			log.debug({
				title : "email",
				details : emailId
			});
			
			var entityId=poRecord.getValue({
				fieldId : 'entity'
			});
			
			log.debug({
			   	title : "entityId",
			   	details : entityId
			});
			
			if(emailId.equals('')){
				log.debug({
					title : "Test",
					details : 'Test'
				});
				//Getting the field values
	    		var vendorRec = search.lookupFields({
	                type: record.Type.VENDOR,
	                id: entityId,
	                columns: ['email']
	            });
	    		emailId=vendorRec.email;
	    		log.debug({
					title : "email",
					details : emailId
				});
			}
			
          var contactSearchObj = search.create({
				   type: "contact",
				   filters: [
				      ["email","is",emailId], 
				      "AND", 
				      ["company.internalidnumber","equalto",entityId]
				   ],
				   columns: [
				      search.createColumn({
				         name: "entityid",
				         sort: search.Sort.ASC
				      })
				   ]
				});
				var searchResultCount = contactSearchObj.runPaged().count;
	 			var contactResults = contactSearchObj.run().getRange({
	     			start:0,
	     			end:1
	     		});
          
	 			var vendorName=null;
	 			if(searchResultCount>0) {
	 				
	 				vendorName= contactResults[0].getValue({
	 				    name: 'entityid',
	 				});
	  
	 			}
				
	 		log.debug({
			   	title : "vendorName",
			   	details : vendorName
			});
			
			var subject='Purchase Order';
			var msg=null;
			if(vendorName!= null){
				msg='Hi '+vendorName+',<br><br>Please find the attachment.';
			}
			else{
				msg='Hi,<br><br>Please find the attachment.';
			}
			
			log.debug({
		   		title : "Email content",
		   		details : msg
		   	});
			
			var DATA_TO_ADD = getXMLData(count, xml,itemArray,po_number,entity,delMethod,freight,incoTerm,order_date,currency_type,name,total);
			
			var objXlsFile = generateDataFile(DATA_TO_ADD, encode, file, po_number);
			
			logme("fileId",objXlsFile);
			
			redirect.toRecord({
				type : record.Type.MESSAGE,

				id : null,
				parameters : {
				
				transaction : recordid,	
				entity : entityId,
				l : 'T',
				templatetype : 'EMAIL',
				entitytype : 'custjob',
				recordtype : 1,
				iscustom : 't',
				recepient: emailId,
				subject: subject,
				msg: msg,
				mediaitem: objXlsFile
				}
				});
						

		} catch (err) {
			logme("ERROR", err);
		}

	}

	return {
		onRequest : onRequest
	};

});

/*******************************************************************************
 * Log these data
 * 
 * @param title
 * @param details
 * @returns
 * 
 * Created on 09-Aug-2017
 */
function logme(title, details) {
	log.debug({
		title : title,
		details : details
	});
}

/*******************************************************************************
 * Creating the XML code of the purchase order in excel format and returning that code
 */
function getXMLData(count,xml,itemArray,po_number,entity,delMethod,freight,incoTerm,order_date,currency_type,name,total) {
	
	var XML = '';
	XML=XML+'<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">';
	XML = XML+ '<Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font ss:FontName="Times New Roman" x:CharSet="204" ss:Color="#000000"/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID="m100857700"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000" ss:Bold="1"/> <Interior/> </Style> <Style ss:ID="m100858944"> <Alignment ss:Horizontal="Center" ss:Vertical="Center"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#FFFFFF" ss:Bold="1"/> <Interior ss:Color="#AAAAAA" ss:Pattern="Solid"/> </Style> <Style ss:ID="m100858964"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:ShrinkToFit="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat ss:Format="d/m/yyyy;@"/> </Style> <Style ss:ID="m100858984"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11"/> <Interior/> </Style> <Style ss:ID="m100859004"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="11" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="m100859024"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="11" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="m100859044"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:ShrinkToFit="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat ss:Format="Fixed"/> </Style> <Style ss:ID="m100859064"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:ShrinkToFit="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat ss:Format="Fixed"/> </Style> <Style ss:ID="m100859084"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:ShrinkToFit="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> <Interior/> <NumberFormat ss:Format="Fixed"/> </Style> <Style ss:ID="m33124452"> <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#FFFFFF" ss:Bold="1"/> <Interior ss:Color="#AAAAAA" ss:Pattern="Solid"/> </Style> <Style ss:ID="m33124472"> <Alignment ss:Horizontal="Center" ss:Vertical="Center"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#FFFFFF" ss:Bold="1"/> <Interior ss:Color="#AAAAAA" ss:Pattern="Solid"/> </Style> <Style ss:ID="m33124492"> <Alignment ss:Horizontal="Center" ss:Vertical="Center"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#FFFFFF" ss:Bold="1"/> <Interior ss:Color="#AAAAAA" ss:Pattern="Solid"/> </Style> <Style ss:ID="m33124512"> <Alignment ss:Horizontal="Center" ss:Vertical="Center"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#FFFFFF" ss:Bold="1"/> <Interior ss:Color="#AAAAAA" ss:Pattern="Solid"/> </Style> <Style ss:ID="m33124532"> <Alignment ss:Horizontal="Center" ss:Vertical="Center"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#FFFFFF" ss:Bold="1"/> <Interior ss:Color="#AAAAAA" ss:Pattern="Solid"/> </Style> <Style ss:ID="s62"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders/> <Interior/> </Style> <Style ss:ID="s64"> <Alignment ss:Horizontal="Left" ss:Vertical="Center"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="16" ss:Bold="1"/> <Interior/> </Style> <Style ss:ID="s67"> <Alignment ss:Horizontal="Center" ss:Vertical="Top"/> <Borders/> <Interior/> </Style> <Style ss:ID="s70"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="18" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s72"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="12" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s75"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="8" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s77"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> <NumberFormat ss:Format="Short Date"/> </Style> <Style ss:ID="s78"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s80"> <Alignment ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="13" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s81"> <Alignment ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s82"> <Alignment ss:Horizontal="Center" ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s83"> <Alignment ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="8" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s84"> <Alignment ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> <NumberFormat ss:Format="Short Date"/> </Style> <Style ss:ID="s86"> <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#FFFFFF" ss:Bold="1"/> <Interior ss:Color="#AAAAAA" ss:Pattern="Solid"/> </Style> <Style ss:ID="s142"> <Alignment ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11"/> <Interior/> </Style> <Style ss:ID="s143"> <Alignment ss:Vertical="Top"/> <Borders> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="11" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s145"> <Alignment ss:Horizontal="Center" ss:Vertical="Top"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s148"> <Alignment ss:Horizontal="Center" ss:Vertical="Top"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Interior/> <NumberFormat ss:Format=""$"#,##0_);[Red]\("$"#,##0\)"/> </Style> <Style ss:ID="s150"> <Alignment ss:Horizontal="Left" ss:Vertical="Top"/> <Borders/> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s151"> <Alignment ss:Vertical="Top"/> <Borders/> <Interior/> </Style> <Style ss:ID="s153"> <Alignment ss:Horizontal="Center" ss:Vertical="Top"/> <Borders> <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> </Borders> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> </Style> <Style ss:ID="s156"> <Alignment ss:Horizontal="Center" ss:Vertical="Center"/> <Borders/> <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Color="#000000"/> <Interior/> </Style> </Styles>';
	XML=XML+'<Worksheet ss:Name="Customer Statement"> <Table ss:ExpandedColumnCount="100" ss:ExpandedRowCount="100" x:FullColumns="1" x:FullRows="1" ss:StyleID="s62" ss:DefaultColumnWidth="40.5"> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="58.5"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="75"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="44.25"/> <Column ss:Index="5" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="54.75"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="37.5"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="42"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="30.75"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="57"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="52.5"/> <Column ss:Index="12" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="30"/> <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="61.5"/> <Row ss:AutoFitHeight="0" ss:Height="24"> <Cell ss:Index="13" ss:MergeAcross="3" ss:StyleID="s64"><Data ss:Type="String">PURCHASE ORDER</Data></Cell> </Row> <Row ss:AutoFitHeight="0" ss:Height="32.0625"> <Cell ss:MergeDown="1" ss:StyleID="s67"><Data ss:Type="String">';
	XML=XML+'</Data></Cell> <Cell ss:Index="13" ss:MergeAcross="3" ss:StyleID="s64"><Data ss:Type="String">'+po_number+'</Data></Cell> </Row> <Row ss:AutoFitHeight="0" ss:Height="32.0625"> <Cell ss:Index="13" ss:MergeAcross="3" ss:StyleID="s70"><Data ss:Type="String">'+currency_type+'  '+total+'</Data></Cell> </Row> <Row ss:AutoFitHeight="0" ss:Height="23.0625"/> <Row ss:AutoFitHeight="0" ss:Height="25.5"> <Cell ss:Index="2" ss:MergeAcross="1" ss:StyleID="s72"><Data ss:Type="String">ORDER DATE</Data></Cell> <Cell ss:Index="5" ss:StyleID="s72"><Data ss:Type="String">SUPPLIER DETAILS</Data></Cell> <Cell ss:Index="9" ss:MergeAcross="1" ss:StyleID="s72"><Data ss:Type="String">DELIVERY METHOD</Data></Cell> <Cell ss:Index="12" ss:MergeAcross="1" ss:StyleID="s72"><Data ss:Type="String">FREIGHT TYPE</Data></Cell> <Cell ss:Index="15" ss:MergeAcross="1" ss:StyleID="s72"><Data ss:Type="String">INCO TERMS</Data></Cell></Row> <Row ss:AutoFitHeight="0"> <Cell ss:Index="2" ss:MergeAcross="1" ss:StyleID="s75"><Data ss:Type="String">'+order_date+'</Data></Cell> <Cell ss:Index="5" ss:MergeAcross="2" ss:StyleID="s75"><Data ss:Type="String">'+name+'</Data></Cell> <Cell ss:Index="9" ss:MergeAcross="1" ss:StyleID="s77"><Data ss:Type="String">'+delMethod+'</Data></Cell> <Cell ss:Index="12" ss:MergeAcross="1" ss:StyleID="s78"><Data ss:Type="String">'+freight+'</Data></Cell> <Cell ss:Index="15" ss:MergeAcross="1" ss:StyleID="s75"><Data ss:Type="String">'+incoTerm+'</Data></Cell><Cell ss:StyleID="s80"/> <Cell ss:StyleID="s81"/> <Cell ss:StyleID="s80"/> <Cell ss:StyleID="s80"/> <Cell ss:StyleID="s80"/> <Cell ss:StyleID="s80"/> </Row> <Row ss:AutoFitHeight="0"> <Cell ss:Index="9" ss:StyleID="s81"/> <Cell ss:StyleID="s81"/> <Cell ss:Index="12" ss:StyleID="s81"/> <Cell ss:StyleID="s81"/> <Cell ss:Index="16" ss:StyleID="s81"/> <Cell ss:StyleID="s81"/> <Cell ss:StyleID="s80"/> <Cell ss:StyleID="s80"/> <Cell ss:StyleID="s80"/> <Cell ss:StyleID="s80"/> </Row> <Row ss:AutoFitHeight="0"> <Cell ss:Index="4" ss:StyleID="s82"/> <Cell ss:Index="6" ss:StyleID="s82"/> <Cell ss:Index="8" ss:StyleID="s82"/> <Cell ss:StyleID="s83"/> <Cell ss:StyleID="s81"/> <Cell ss:Index="12" ss:StyleID="s81"/> <Cell ss:StyleID="s81"/> <Cell ss:Index="16" ss:StyleID="s84"/> <Cell ss:StyleID="s84"/> <Cell ss:Index="20" ss:MergeAcross="1" ss:StyleID="s82"/> </Row> <Row ss:AutoFitHeight="0"/> <Row ss:AutoFitHeight="0"> <Cell ss:Index="2" ss:StyleID="s86"><Data ss:Type="String">CODE</Data></Cell> <Cell ss:MergeAcross="2" ss:StyleID="m33124452"><Data ss:Type="String">SUPPLIER CODE</Data></Cell> <Cell ss:MergeAcross="1" ss:StyleID="m33124472"><Data ss:Type="String">ITEM NAME</Data></Cell> <Cell ss:MergeAcross="1" ss:StyleID="m33124492"><Data ss:Type="String">BARCODE</Data></Cell> <Cell ss:MergeAcross="1" ss:StyleID="m33124512"><Data ss:Type="String">QTY</Data></Cell> <Cell ss:MergeAcross="1" ss:StyleID="m33124532"><Data ss:Type="String">COST</Data></Cell> <Cell ss:MergeAcross="1" ss:StyleID="m100858944"><Data ss:Type="String">SUB TOTAL</Data></Cell> <Cell ss:Index="17" ss:StyleID="s78"/> <Cell ss:Index="19" ss:StyleID="s78"/> <Cell ss:Index="21" ss:StyleID="s78"/> </Row>';
	

	for (var i = 0; i <count; i++) {
		//log.debug({title : "itemArray[i].itemCode",details : itemArray[i]});
		XML=XML+'<Row ss:AutoFitHeight="0" ss:Height="15"> <Cell ss:Index="2" ss:MergeDown="1" ss:StyleID="m100858964"><Data ss:Type="String">'+itemArray[i].itemCode+'</Data></Cell> <Cell ss:MergeAcross="2" ss:MergeDown="1" ss:StyleID="m100858984"><Data ss:Type="String">'+itemArray[i].vendorCode+'</Data></Cell> <Cell ss:MergeAcross="1" ss:MergeDown="1" ss:StyleID="m100859004"><Data ss:Type="String">'+itemArray[i].itemName+'</Data></Cell> <Cell ss:MergeAcross="1" ss:MergeDown="1" ss:StyleID="m100859024"><Data ss:Type="String">'+itemArray[i].upcCode+'</Data></Cell> <Cell ss:MergeAcross="1" ss:MergeDown="1" ss:StyleID="m100859044"><Data ss:Type="String">'+itemArray[i].quantity+'</Data></Cell> <Cell ss:MergeAcross="1" ss:MergeDown="1" ss:StyleID="m100859064"><Data ss:Type="String">'+itemArray[i].rate+'</Data></Cell> <Cell ss:MergeAcross="1" ss:MergeDown="1" ss:StyleID="m100859084"><Data ss:Type="String">'+itemArray[i].amount+'</Data></Cell> </Row> <Row ss:AutoFitHeight="0" ss:Height="15"/>';
		
		} 
	XML=XML+'<Row ss:AutoFitHeight="0" ss:Height="21"> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> <Cell ss:Index="10" ss:StyleID="s143"/> <Cell ss:MergeAcross="2" ss:StyleID="m100859004"> <Data ss:Type="String">SALES AMOUNT</Data> </Cell> <Cell ss:MergeAcross="1" ss:StyleID="m100859084"> <Data ss:Type="String">'+currency_type+'  '+total+'</Data> </Cell> </Row> <Row ss:AutoFitHeight="0" ss:Height="22.5"> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s150"/> <Cell ss:StyleID="s150"/> <Cell ss:Index="10" ss:StyleID="s151"/> <Cell ss:MergeAcross="2" ss:StyleID="m100859004"> <Data ss:Type="String">BALANCE DUE</Data> </Cell> <Cell ss:MergeAcross="1" ss:StyleID="m100859084"> <Data ss:Type="String">'+currency_type+'  '+total+'</Data> </Cell> </Row> <Row ss:AutoFitHeight="0" ss:Height="14.25"> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> </Row> <Row ss:AutoFitHeight="0" ss:Height="14.25"> <Cell ss:StyleID="s142"/> <Cell ss:StyleID="s142"/> </Row> <Row > <Cell ss:StyleID="s78"/> <Cell ss:StyleID="s156"> <Data ss:Type="String">Telegram Co</Data> </Cell> <Cell ss:StyleID="s78"/> <Cell ss:MergeAcross="11" ss:MergeDown="3" ss:StyleID="m100857700"> <Data ss:Type="String">NOTES</Data> </Cell> </Row> <Row> <Cell ss:StyleID="s78"/> <Cell ss:StyleID="s156"> <Data ss:Type="String">63A Little Oxford Street</Data> </Cell> <Cell ss:StyleID="s78"/> </Row> <Row> <Cell ss:StyleID="s78"/> <Cell ss:StyleID="s156"> <Data ss:Type="String">Collingwood VIC 3066</Data> </Cell> <Cell ss:StyleID="s78"/> </Row> <Row> <Cell ss:StyleID="s78"/> <Cell ss:StyleID="s156"> <Data ss:Type="String">P: +61 3 9318 0822</Data> </Cell> <Cell ss:StyleID="s78"/> </Row> <Row> <Cell ss:StyleID="s78"/> <Cell ss:StyleID="s156"> <Data ss:Type="String">service@telegramco.com</Data> </Cell> <Cell ss:StyleID="s78"/> </Row> <Row> <Cell ss:StyleID="s78"/> <Cell ss:StyleID="s156"> <Data ss:Type="String">www.telegramco.com</Data> </Cell> <Cell ss:StyleID="s78"/> </Row> <Row > <Cell ss:Index="2" ss:StyleID="s78"> <Data ss:Type="String">ABN: 50 606 166 741</Data> </Cell> </Row> </Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <Selected/> <Panes> <Pane> <Number>3</Number> <ActiveRow>2</ActiveRow> <ActiveCol>2</ActiveCol> </Pane> </Panes> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> </Workbook>';
	
return XML;

}

/*******************************************************************************
 * Creating Excel file of the purchase order and store the file in a folder and returning the file id
 */
function generateDataFile(data, encode, file,po_no) {
	var strXmlEncoded = encode.convert({
		string : data,
		inputEncoding : encode.Encoding.UTF_8,
		outputEncoding : encode.Encoding.BASE_64
	});

	var objXlsFile = file.create({
		name : 'Excel_'+po_no+'_'+Date.now()+'.xls',
		fileType : file.Type.EXCEL,
		contents : strXmlEncoded
	});
	objXlsFile.folder = 194609;
	var fileId = objXlsFile.save();
	//logme('fileId',fileId);
	return fileId;

}

/*******************************************************************************
 * Fix text
 * 
 * @param argument
 * @returns Created By rosemol on 15-Nov-2017 4:30:21 PM
 */
function escape_for_xml(argument, xml) {
	if (argument != "" && argument != null) {
		var newString = xml.escape({
			xmlText : argument
		});
		
		newString = newString.replace(/&/g, '&');
        newString = newString.replace(/</g, '<');
        newString = newString.replace(/>/g, '>');
        newString = newString.replace(/"/g, '"');
        newString = newString.replace(/'/g, ''');
		return newString;
	} else {
		return "";
	}

}

User Event script for setup Email

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 * @author Jobin and Jismi IT services LLP
 * @appliedtorecord Message
 * 
 */

/**
 * Script Description: This script for setting the recipient, subject, message and attaching the created excel file in email message   
 */

/*******************************************************************************
 * * Telegram* *
 * **************************************************************************
 * Date:9/2/18 
 * Script name: TG UE Attach Excel
 * Script id: customscript_tg_ue_attach_excel
 * Deployment id: customdeploy_tg_ue_attach_excel
 * Applied to: Message Record
 * 

 ******************************************************************************/

define(
		[ 'N/email', 'N/http', 'N/https', 'N/record', 'N/runtime',
				'N/ui/serverWidget', 'N/url', 'N/redirect' ],

		function(email, http, https, record, runtime, serverWidget, url,
				redirect) {
			/**
			 * Function definition to be triggered before record is loaded.
			 * 
			 * @param {Object}
			 *            scriptContext
			 * @param {Record}
			 *            scriptContext.newRecord - New record
			 * @param {string}
			 *            scriptContext.type - Trigger type
			 * @param {Form}
			 *            scriptContext.form - Current form
			 * @Since 2015.2
			 */

			function beforeLoad(scriptContext) {
				try {
					
					var entryString = scriptContext.newRecord.getValue({
						fieldId : 'entryformquerystring'
					});
					var indexOfiscustom = entryString.indexOf('iscustom');
					log.debug({
						title : 'b4loadindexOfiscustom',
						details : indexOfiscustom
					});
					log.debug({
						title : 'b4loadentryString',
						details : entryString
					});
					if (indexOfiscustom > 0) {
						var request = scriptContext.request;

						var isCustom = request.parameters.iscustom;
						var emailId = request.parameters.recepient;
						var mediaitem = request.parameters.mediaitem;
						var msg = request.parameters.msg;
						
						//Check whether message created is custom
						if (isCustom == 't') {
							log.debug({
								title : "isCustom",
								details : isCustom
							});

							var objRecord = scriptContext.newRecord;
							
							log.debug({
								title : "recipientemail",
								details : emailId
							});
							
							log.debug({
								title : "msg",
								details : msg
							});
							
							//Set the recipient mail id
							objRecord.setText({
							    fieldId: 'recipientemail',
							    text: emailId,
							    ignoreFieldChange: true
							});
							
							//Set the Message body
							objRecord.setText({
							    fieldId: 'message',
							    text: msg,
							    ignoreFieldChange: true
							});
						
							//Attaching the created Excel file in the email
							objRecord.setSublistValue({
							    sublistId: 'mediaitem',
							    fieldId: 'mediaitem',
							    value: mediaitem,
							    line:0,
							    ignoreFieldChange: true
							});
							
							function logme(title, details) {
								log.debug({
									title : title,
									details : details
								});
							}
						} 
					}

				} catch (e) {
					log.debug({
						title : 'EmailWidget',
						details : e.message
					});
				}

			}	
			
			return {
				beforeLoad : beforeLoad
			};

		});

Leave a comment

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