BOL PDF Print From Sales Order.

Jira code: AN-65

This article briefs about a custom bill of lading printed out. It shows search/view for sales orders that displays a column for “Bill of Lading” – this column will have a “print” link that creates a PDF. When they print this bill of lading, the following form pops up – this is the Bill of Lading that we’d like to see.  This should be something that can be made for a SO that has any status (open, closed, pending approval, etc.)

User Event to update BOL field

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

/**
 * Script Description: This script is for setting a buttons in the Sales Order record
 */

/*******************************************************************************
 * * AN * *
 * **************************************************************************
 * Date:02/11/18 
 * Script name: AN UE SO Buttons
 * Script id: customscript_an_ue_so_buttons
 * Deployment id: customdeploy_an_ue_so_buttons
 * Applied to: Sales order
 * 

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

define(['N/search','N/url','N/record'],

function(search,url,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 {
    		
    		
    		var CustRec=scriptContext.form;
    		log.debug({    
				title: 'CustRec',
                details: scriptContext.newRecord
            });
    		var currRec= scriptContext.newRecord;
    		var status = currRec.getValue({
    			fieldId:'orderstatus'
    		});
    		
    		//Loading Client Script
			CustRec.clientScriptFileId = 10458;
			
    		if(scriptContext.type == 'view'){
    			
    			
    			
    			
    			var recId=scriptContext.newRecord.id;
    			log.debug({    
    				title: 'recId',
                    details: recId
                });
    			
              	
              //Set Shipping PDF button
    			var shippingPDF=CustRec.addButton({
    				id:'custpage_shipping_pdf_btn',
    				label:'Shipping Tag',
    				functionName:'shippingPDF'
    			});
    			// AJ MOD on 14/05/2019
    			
    			if(status=='A')
    			{ //Set Shipping PDF button
    			var markAll=CustRec.addButton({
    				id:'custpage_mark_all_swo_v',
    				label:'Mark All SWO',
    				functionName:'markAllSwo'
    			});
    			}
    		    
    		}else if(scriptContext.type == 'edit'){
    			if(status=='A')
    			{
    			 //Set Shipping PDF button
    			var markAll=CustRec.addButton({
    				id:'custpage_mark_all_swo',
    				label:'Mark All SWO',
    				functionName:'markAllSwoEdit'
    			});
    			}
    		}
    	
			
		} catch (e) {
			
			log.debug({    
				title: e.name,
                details: e.message
            });
		}

    }
    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {Record} scriptContext.oldRecord - Old record
     * @param {string} scriptContext.type - Trigger type
     * @Since 2015.2
     */
    function afterSubmit(scriptContext) {
    	try{
    		var id = scriptContext.newRecord.id;
    		
    		log.debug("id",id)
    		
    		var toSL = url.resolveScript({
    		    scriptId: 'customscript_an65_jj_sl_print_bol_so',
    		    deploymentId: 'customdeploy_an65_jj_sl_print_bol_so',
    		    params:{
    		    	recId:id
    		    }
    		//    returnExternalUrl: true
    		});
    		
    		
    		// to load the rec
    		var curRec=record.load({
    			type:'salesorder',
    			id:id
    		
    		})
    		// to set the value
    		curRec.setValue({
    			fieldId:'custbody_bill_of_landing',
    			value:toSL
    		})
    		curRec.save({ignoreMandatoryFields:true});
    	}catch(e)
    	{
    		log.debug("Err @ afterSubmit submit",e)
    	}

    }
   

    return {
        beforeLoad: beforeLoad,
        afterSubmit:afterSubmit
    };
    
});

Suitelet

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:AURORA NATURALS AN-65 Print BOL
 * 
  *************************************************************************
 * Date : 13/05/2019
 * 
 * Author: Jobin & Jismi IT Services LLP Script Description : To Print BOL From
 * SO Date created : 13/05/2019
 * 
 * REVISION HISTORY
 * 
 * Revision 1.0 ${13/05/2019} aj : created
 * 
 * 
 ******************************************************************************/
define(
		[ 'N/file', 'N/render', 'N/record', 'N/search' ],

		function(file, render, record, search) {
			
			
			/*****
			 * searchForIF
			 */
			function searchForIF(item,soId)
			{
				try{
					
					/***********************************************************
					 * Search for item lines
					 */
					var salesorderSearchObj = search.create({
						type : "salesorder",
						filters : [
								[ "type", "anyof", "SalesOrd" ],
								"AND",
								[ "internalidnumber", "equalto", soId ],
								 "AND", 
							      ["item","anyof",item], 
								"AND",
								
								[ "applyingtransaction.type", "anyof",
										"ItemShip" ] ],
						columns : [ search.createColumn({
							name : "item",
							label : "Item"
						}), search.createColumn({
							name : "applyingtransaction",
							label : "Applying Transaction"
						}), search.createColumn({
							name : "salesdescription",
							join : "item",
							label : "Description"
						}), search.createColumn({
							name : "custcol_item_origin",
							label : "Origin"
						}), search.createColumn({
							name : "quantity",
							label : "Quantity"
						}) ,
						search.createColumn({
					         name: "type",
					         join: "applyingTransaction",
					         label: "Type"
					      }),
      search.createColumn({
         name: "quantity",
         join: "applyingTransaction",
         label: "Quantity"
      })
						]
					});
					var searchResultCount = salesorderSearchObj.runPaged().count;
					// log.debug("salesorderSearchObj result count",
					// searchResultCount);
					var lotNumber="";
					var salesorderSearchResult = salesorderSearchObj.run()
							.getRange({
								start : 0,
								end : 100
							});

					var object = {};
					for (var j = 0; j < salesorderSearchResult.length; j++) {

						// ujna
						
						var applyingType = salesorderSearchResult[j].getText({
					         name: "type",
					         join: "applyingTransaction",
					         label: "Type"
					      })
					      log.debug("applyingType",applyingType)
					      
					    	var ifNum = salesorderSearchResult[j].getValue({
									name : "applyingtransaction",
									label : "Applying Transaction"
								})
								
							

								// to get the LOT NUMBER

								if (checkif(item) && checkif(ifNum))
									lotNumber = lotNumber+ getLotNumberOfItem(item, ifNum);
								else
									lotNumber = lotNumber+"";

		
					    	  
						log.debug("lotNumber",lotNumber);
						

					}
					

					return lotNumber;
					
				}catch(e)
				{
					log.debug(" Err @ searchForIF",e)
                  log.error(" Err @ searchForIF",e)
				}
			}

			// To check whether a value exists in parameter
			function checkif(parameter) {
				if (parameter != null && parameter != undefined
						&& parameter != false && parameter != "null"
						&& parameter != "undefined" && parameter != "false"
						&& parameter != "" && parameter != " ") {
					return true;
				}
			}
			/*******************************************************************
			 * 
			 */
			function getLotNumberOfItem(item, ifNum) {
				try {
					var lotNumber='';
					var itemfulfillmentSearchObj = search.create({
						type : "itemfulfillment",
						filters : [ [ "type", "anyof", "ItemShip" ], "AND",
								[ "internalidnumber", "equalto", ifNum ],
								"AND",
								[ "item.internalidnumber", "equalto", item ]
						/ ,"AND", [ "mainline", "is", "T" ] /],
						columns : [ search.createColumn({
							name : "inventorynumber",
							join : "inventoryDetail",
							label : " Number"
						}) ]
					});
					var searchResultCount = itemfulfillmentSearchObj.runPaged().count;
					if (searchResultCount > 0) {
						var itemfulfillmentSearchResult = itemfulfillmentSearchObj
								.run().getRange({
									start : 0,
									end : 1000
								});

						for (var j = 0; j < itemfulfillmentSearchResult.length; j++) {

							
						lotNumber =lotNumber+ itemfulfillmentSearchResult[j].getText({
							name : "inventorynumber",
							join : "inventoryDetail",
							label : " Number"
						})+' <br /> ';
						}

					}

					return lotNumber;

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

			function onRequest(context) {
				try {
					var soId = context.request.parameters.recId;
					var itemArr = [], items = [];
					var itemObj = {};
					
					
					/***********************************************************
					 * Search for item lines
					 */
					var objRecord = record.load({
						type : 'salesorder',
						id : soId
					});
					
					//AJ MOD ON 27.05.2019
					
					var lineNum = objRecord.getLineCount({
					    sublistId: 'item'
					});
					
					var items=[];
					for(var i=0;i<lineNum;i++)
						{
						var itemObj={};
						var itemID =objRecord.getSublistValue({
						    sublistId: 'item',
						    fieldId: 'item',
						    line: i
						});
						var item=objRecord.getSublistText({
						    sublistId: 'item',
						    fieldId: 'custcol_item_id_line',
						    line: i
						});
						var qty=objRecord.getSublistValue({
						    sublistId: 'item',
						    fieldId: 'quantity',
						    line: i
						});
						
						var origin=objRecord.getSublistValue({
						    sublistId: 'item',
						    fieldId: 'custcol_item_origin',
						    line: i
						});
						
						var description=objRecord.getSublistValue({
						    sublistId: 'item',
						    fieldId: 'description',
						    line: i
						});
						
						
						// to get the item details
						var lotNumbers = searchForIF(itemID,soId);
						log.debug("lotNumbers inn rec",lotNumbers);
						
						
						itemObj = {
								item : item,
								itemID : itemID,
								lotNumber : lotNumbers,
								qty : qty,
								origin : origin,
								description : description
							};
							items.push(itemObj);
						
						}
				

					var myFile = render.create();

					var template = 114;

					myFile.setTemplateById(template);

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

					/* Modified on 17 May 2019  /

					myFile.addCustomDataSource({
						format : render.DataSource.JSON,
						alias : "items",
						data : JSON.stringify({
							items : items
						}).replace(/&/g,'&')
					});

					var transactionFile = myFile.renderAsPdf();

					context.response.writeFile(transactionFile, true);

				} catch (error) {
					log.error('error @ onRequest', error)
					log.debug('error @ onRequest', error)
				}

			}

			return {
				onRequest : onRequest
			};

		});

Advanced PDF

<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<head>
  <#assign aDateTime = .now>
<#assign aDate = aDateTime?date>
  
	<link name="NotoSans" type="font" subtype="truetype" src="${nsfont.NotoSans_Regular}" src-bold="${nsfont.NotoSans_Bold}" src-italic="${nsfont.NotoSans_Italic}" src-bolditalic="${nsfont.NotoSans_BoldItalic}" bytes="2" />
	<#if .locale == "zh_CN">
		<link name="NotoSansCJKsc" type="font" subtype="opentype" src="${nsfont.NotoSansCJKsc_Regular}" src-bold="${nsfont.NotoSansCJKsc_Bold}" bytes="2" />
	<#elseif .locale == "zh_TW">
		<link name="NotoSansCJKtc" type="font" subtype="opentype" src="${nsfont.NotoSansCJKtc_Regular}" src-bold="${nsfont.NotoSansCJKtc_Bold}" bytes="2" />
	<#elseif .locale == "ja_JP">
		<link name="NotoSansCJKjp" type="font" subtype="opentype" src="${nsfont.NotoSansCJKjp_Regular}" src-bold="${nsfont.NotoSansCJKjp_Bold}" bytes="2" />
	<#elseif .locale == "ko_KR">
		<link name="NotoSansCJKkr" type="font" subtype="opentype" src="${nsfont.NotoSansCJKkr_Regular}" src-bold="${nsfont.NotoSansCJKkr_Bold}" bytes="2" />
	<#elseif .locale == "th_TH">
		<link name="NotoSansThai" type="font" subtype="opentype" src="${nsfont.NotoSansThai_Regular}" src-bold="${nsfont.NotoSansThai_Bold}" bytes="2" />
	</#if>
    <macrolist>
        <macro id="nlheader">
       
         
            <table style="width: 100%; font-size: 10pt;"><tr>
	<td rowspan="4" style="padding: 0;"><img src="https://system.na3.netsuite.com/core/media/media.nl?id=10877&c=1313499&h=9b939ca23701b30441fb" style="width: 70%; height: 70%;" /></td>
	<td colspan="3" rowspan="4" style="font-size:9pt;align:center">Aurora Products, Inc.<br />205 Edison Road (PEZ BLVD)<br />Orange, CT 06477<br />P:203.375.9956 F:203.375.9734<br /><br />Thank you for your order!!!</td>
	<td align="right" colspan="2" style="padding: 0;"><span><b style="font-size: 16pt;">Bill Of Lading    </b><br /><b style="font-size: 10pt;">From SO: ${record.tranid}<br />Date: ${aDate} <br /></b></span><b>Page <pagenumber/> of <totalpages/></b></td>
	</tr>
             
           
           </table>
        </macro>
        <macro id="nlfooter">
             
        </macro>
    </macrolist>
    <style type="text/css">* {
		<#if .locale == "zh_CN">
			font-family: NotoSans, NotoSansCJKsc, sans-serif;
		<#elseif .locale == "zh_TW">
			font-family: NotoSans, NotoSansCJKtc, sans-serif;
		<#elseif .locale == "ja_JP">
			font-family: NotoSans, NotoSansCJKjp, sans-serif;
		<#elseif .locale == "ko_KR">
			font-family: NotoSans, NotoSansCJKkr, sans-serif;
		<#elseif .locale == "th_TH">
			font-family: NotoSans, NotoSansThai, sans-serif;
		<#else>
			font-family: NotoSans, sans-serif;
		</#if>
		}
		table {
			font-size: 9pt;
			table-layout: fixed;
		}
        th {
            font-weight: bold;
            font-size: 8pt;
            vertical-align: middle;
            padding: 5px 6px 3px;
            background-color: #e3e3e3;
            color: #333333;
        }
        td {
            padding: 4px 6px;
        }
		td p { align:left }
      .headOfItem{
         font-weight: bold;
            font-size: 8pt;
            vertical-align: middle;
      }
      .itemtable tr td{
        border:1;
      }
</style>
</head>
<body header="nlheader" header-height="15%" footer="nlfooter" footer-height="20pt" padding="0.5in 0.5in 0.5in 0.5in" size="Letter">
    <table style="width: 100%; margin-top: 10px;"><tr>
	<td style="font-size: 8pt;width:10%"><b>${record.billaddress@label} :</b></td>
	<td style="padding: 0;width:35%">${record.billaddress}</td>
	<td style="font-size: 8pt; width:10%;"><b>${record.shipaddress@label} :</b></td>
	<td style="padding: 0;width:35%;">${record.shipaddress}</td>
	</tr></table>

<table style="width: 100%; margin-top: 10px;"><tr>
	<td style="font-size:10pt;font-weight:bold;">Customer Instructions</td>
	<td style="font-size:10pt;font-weight:bold;">Special Instructions</td>
	</tr>
	<tr>
	<td style="padding-top: 2px;">${record.custbody_ship_instruction}</td>
	<td style="padding-top: 2px;">${record.custbody_spe_instruction}</td>
	</tr></table>
<br /><#if record.item?has_content>
<table class="itemtable" style="width: 100%; border:1;"><!-- start items --><#list items.items as item><#if item_index==0><tr>
	<td class="headOfItem"  style="width: 20%;">Part Number</td>
	<td class="headOfItem" style="border-right: 0; width: 40%;">Description</td>
	<td class="headOfItem" style="border-right: 0;border-left: 0;width: 10%;">COO</td>
	<td class="headOfItem" style="border-left: 0;align:center; width: 10%;">Quantity</td>
	
	<td class="headOfItem" style="width: 20%;">Lot Number</td>
	</tr>
	</#if>
	<tr>
    
        
     <td style="width: 20%;">${item.item}</td>    
	
	<td style="border-right: 0; width: 40%;">${item.description}</td>
	<td style="border-right: 0;border-left: 0; width: 10%;">${item.origin}</td>
	<td style="border-left: 0;align:center; width: 10%;">${item.qty}</td>
	
	<td style="width: 20%;">${item.lotNumber}</td>
	</tr>
	</#list><!-- end items --></table>
</#if>

<table style="page-break-inside: avoid; width: 100%; margin-top: 10px; border:0"><tr>
	<td align="center" font-size="11pt;"><b>Total Qty:</b> ${record.custbody_total_qty_case_so}</td>
	</tr></table>
</body>
</pdf>

Leave a comment

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