Jira code: AN-2 Task 1
Place one Raw Material Tag button in the PO record. This button is only set in the PO having Item Receipt. On that button click, it will create the Raw material tag PDF with the data from the PO as well as the data from the Item Receipt. The raw material tag includes details such as Product, Date Received, Lot Number, COO and PO#.
User Event Script
/**
* @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 PO Buttons
* Script id: customscript_an_ue_po_buttons
* Deployment id: customdeploy_an_ue_po_buttons
* Applied to: Purchase order
*
******************************************************************************/
define(['N/search'],
function(search) {
/**
* 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;
if(scriptContext.type == 'view'){
//Loading Client Script
CustRec.clientScriptFileId = 10169;
var recId=scriptContext.newRecord.id;
log.debug({
title: 'recId',
details: recId
});
var itemreceiptSearchObj = search.create({
type: "itemreceipt",
filters:
[
["type","anyof","ItemRcpt"],
"AND",
["createdfrom.internalidnumber","equalto",recId]
],
columns:
[
search.createColumn({
name: "tranid",
label: "tranid"
}),
search.createColumn({name: "trandate", label: "Date"}),
]
});
var searchResultCount = itemreceiptSearchObj.runPaged().count;
if(searchResultCount>0){
//Set Raw Material Tag button
var rowMaterialTag=CustRec.addButton({
id:'custpage_row_material_tag_btn',
label:'Raw Material Tag',
functionName:'rowMaterialTag'
});
}
}
} 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 button
*/
/*******************************************************************************
* * AN * *
* **************************************************************************
* Date:2/11/18
* Script name: AN CS PO Btn Action
* Script id: customscript_an_cs_po_btn_action
* Deployment id: customdeploy_an_cs_po_btn_action
* Applied to: Purchase order
*
******************************************************************************/
define(['N/currentRecord','N/record','N/url'],
function(currentRecord,record,url) {
/**
* 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) {
try {
console.log("In");
} catch (e) {
console.log(e.name,e.message);
}
}
function rowMaterialTag() {
try {
console.log("rowMaterialTag");
var recID = currentRecord.get().id;
var parameterObj = {
intenalId: recID
};
var recObj = record.load({ type: "purchaseorder", id: recID });
var labelUrl = url.resolveScript({
scriptId: "customscript_sl_raw_material_tag",
deploymentId: "customdeploy_sl_raw_material_tag",
returnExternalUrl: false,
params: parameterObj
});
var childwindow = window.open(labelUrl);
} catch (e) {
console.log(e.name,e.message);
}
}
return {
pageInit: pageInit,
rowMaterialTag: rowMaterialTag
};
});
XML Code
<?xml version="1.0"?>
<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<head>
<macrolist>
<macro id="nlfooter">
<table class="footer" style="width: 100%;">
<tr>
<td align="center" class="pagenumber">
<pagenumber /> of
<totalpages />
</td>
</tr>
</table>
</macro>
</macrolist>
<style type="text/css">
span.title {
font-size: 24pt;
}
span.pagenumber {
font-size: 10pt;
}
span.number {
font-size: 16pt;
}
.pagenumber {
font-size: 6pt;
}
.addressheader {
font-size: 9pt;
}
span.itemname {
font-weight: bold;
line-height: 150%;
}
barcode{
width: 450px;
height:75px;
}
</style>
</head>
<body header="nlheader" header-height="10%" footer="nlfooter" footer-height="10pt" padding="0.35in 0.35in 0.35in 0.35in" size="A4-LANDSCAPE">
<p class="title" align="center" style="font-size:28px;">
<b>Raw Material Tag</b>
</p>
ReplacewithContent
</body>
</pdf>
Suitelet
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
/**
* Script Description: PDF Creation
*/
/*******************************************************************************
* * AN * *
* **************************************************************************
* Date:2/11/18
* Script name: AN SL Raw Material Tag
* Script id: customscript_sl_raw_material_tag
* Deployment id: customdeploy_sl_raw_material_tag
*
******************************************************************************/
define(['N/file','N/render','N/record','N/search'],
function(file,render,record,search) {
/**
* 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 {
var internalId = context.request.parameters.intenalId;
log.debug({
title: "record internalId",
details: internalId
});
var recordObj = record.load({ type: "purchaseorder", id: internalId });
var poNo=recordObj.getText({
fieldId: 'tranid'
});
log.debug({
title: "poNo",
details: poNo
});
var numLines = recordObj.getLineCount({
sublistId: 'item'
});
log.debug({
title: "numLines",
details: numLines
});
var bodyContent = '';
var numpages = 0;
var itemreceiptSearchObj = search.create({
type: "itemreceipt",
filters:
[
["type","anyof","ItemRcpt"],
"AND",
["createdfrom.internalidnumber","equalto",internalId],
"AND",
["inventorydetail.item","noneof","@NONE@"]
],
columns:
[
search.createColumn({
name: "item",
join: "inventoryDetail",
label: "Item"
}),
search.createColumn({name: "trandate", label: "Date"}),
search.createColumn({
name: "inventorynumber",
join: "inventoryDetail",
sort: search.Sort.ASC,
label: " Number"
}),
search.createColumn({
name: "location",
join: "inventoryDetail",
label: "Location"
})
]
});
var searchResultCount = itemreceiptSearchObj.runPaged().count;
log.debug("itemreceiptSearchObj result count",searchResultCount);
var temObject = {};
var start = 0;
var end = 1000;
for (var i = 0; i < Math.ceil(searchResultCount/1000); i++)
{
result = itemreceiptSearchObj.run().getRange({
start: start,
end: end
});
var len=result.length;
for (var j = 0; j < len ; j++) {
var itemReceiptsearchResult = result[j];
var item = itemReceiptsearchResult.getValue({
name: "item",
join: "inventoryDetail"
});
var date = itemReceiptsearchResult.getValue({
name: "trandate"
});
var lotNum = itemReceiptsearchResult.getText({
name: "inventorynumber",
join: "inventoryDetail"
});
var location = itemReceiptsearchResult.getValue({
name: "location",
join: "inventoryDetail"
});
temObject[item] = [date,lotNum,location];
}
start = end;
end = end + 1000;
}
log.debug("temObject",temObject);
for (var i = 0; i < numLines; i++) {
numpages = numpages + 1;
var itemNamev = recordObj.getSublistValue({
fieldId: "item",
sublistId: 'item',
line: i
});
var itemName = recordObj.getSublistText({
fieldId: "item",
sublistId: 'item',
line: i
});
var countryofOrigindtls = null;
if((temObject[itemNamev]!=null) && (temObject[itemNamev]!=undefined) && (temObject[itemNamev]!='')){
var countryofOrigindtls = search.lookupFields({
type: record.Type.LOCATION,
id: temObject[itemNamev][2],
columns: ['country']
});
var receiveDate = temObject[itemNamev][0];
var lotNumber= temObject[itemNamev][1];
var countryofOrigin=countryofOrigindtls.country;
}
var content = '<div style="margin-left: 5%;border:1px;"> <table style="width: 95%; margin-top: 3px;font-size:24px;"> <tr> <td style="width: 40%;"> <b>Product:</b></td> <td> Replacewithitemcode </td> </tr> <tr> <td style="width: 40%;"><b> Date Received:</b></td> <td> Replacewithdate </td> </tr> <tr> <td style="width: 40%;"><b> Lot Number:</b></td> <td> ReplacewithLotNumber </td> </tr> <tr> <td style="width: 40%;"><b> Country of Origin:</b></td> <td> ReplacewithCountryofOrigin </td> </tr> <tr> <td style="width: 40%;"><b> PO#:</b></td> <td> ReplacewithPONo </td> </tr><tr> <td></td> </tr> </table> </div><br></br><br></br>';
if((itemName!=null) && (itemName!=undefined) && (itemName!='')){
content = content.replace("Replacewithitemcode", '<span><b>'+itemName+'</b><br></br><barcode codetype="code128" style="width: 500px;" align="left" showtext="false" value="'+itemName+'"> </barcode></span>');
}
else{
content = content.replace("Replacewithitemcode", '<span></span>');
}
if((receiveDate!=null) && (receiveDate!=undefined) && (receiveDate!='')){
content = content.replace("Replacewithdate", '<span><b>'+receiveDate+'</b><br></br><barcode codetype="code128" align="left" showtext="false" value="'+receiveDate+'"></barcode></span>');
}
else{
content = content.replace("Replacewithdate", '<span></span>');
}
if((lotNumber!=null) && (lotNumber!=undefined) && (lotNumber!='')){
content = content.replace("ReplacewithLotNumber", '<span><span><b>'+lotNumber+'</b></span><br></br><barcode codetype="code128" align="left" showtext="false" value="'+lotNumber+'"></barcode></span>');
}
else{
content = content.replace("ReplacewithLotNumber", '<span></span>');
}
if((countryofOrigin!=null) && (countryofOrigin!=undefined) && (countryofOrigin!='')){
content = content.replace("ReplacewithCountryofOrigin", '<span><span><b>'+countryofOrigin+'</b></span><br></br><barcode codetype="code128" showtext="false" value="'+countryofOrigin+'"> </barcode></span>');
}
else{
content = content.replace("ReplacewithCountryofOrigin", '<span></span>');
}
if((poNo!=null) && (poNo!=undefined) && (poNo!='')){
content = content.replace("ReplacewithPONo", '<span><span><b>'+poNo+'</b></span><br></br><barcode codetype="code128" showtext="false" value="'+poNo+'"> </barcode></span>');
}
else{
content = content.replace("ReplacewithPONo", '<span></span>');
}
bodyContent += content;
}
var myXMLFile = file.load({
id: '10168'
});
var xmlContent = myXMLFile.getContents();
xmlContent = xmlContent.replace("ReplacewithContent",bodyContent);
var pdfFile = render.xmlToPdf({
xmlString: xmlContent
});
pdfFile.name = "rawMaterialTag.pdf"
context.response.writeFile(pdfFile,true);
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
return {
onRequest: onRequest
};
});