Jira Code: PROT – 127
This script triggers when an invoice is created. It will populate the shipping charge and tracking number from the IF to the invoice. The IF details are taken from the URL of the invoice.
define(['N/currentRecord','N/record'],
/**
* @param {record} record
*/
function(currentRecord,record) {
function pageInit(scriptContext) {
try {
// Function used to retrieve the itemship parameter value from the url of the invoice
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
var itemship = getUrlParameter('itemship'); //save the IF number
// Load the IF details and retreive the custom values of tracking number and shipping charge to set it to the invoice.
if(itemship != undefined){ // If Invoice is created from IF there will be itemship value
try {
//Load the IF record
var ifRecord = record.load({
type: record.Type.ITEM_FULFILLMENT,
id: itemship,
isDynamic: true,
});
//get the xpsTrackingNumber
var xpsTrackingNo = ifRecord.getValue({
fieldId : 'custbody_jj_package_tracking_no'
});
console.log('xpsTrackingNo',xpsTrackingNo);
//get the xpsShippingCharge
var xpsShippingCharge = ifRecord.getValue({
fieldId : 'custbody_jj_shipping_charge'
});
console.log('xpsShippingCharge',xpsShippingCharge);
var shippingCost = ifRecord.getValue({
fieldId : 'shippingcost'
});
console.log('shippingCost',shippingCost);
var flatShippingCharge = ifRecord.getValue({
fieldId : 'custbody18'
});
console.log('flatShippingCharge',flatShippingCharge);
} catch (e) {
logme('err@gettingIfvalues',getError(e));
}
try {
//set the xpsTrackingNumber and xpsShippingCharge to Invoice
//set the IF iternal id to the invoice custom field.
var invoiceRecord = scriptContext.currentRecord;
invoiceRecord.setValue({
fieldId : 'custbody_prof127_jj_created_from_if',
value : itemship
});
//set custom xpsShippingCharge value to the Shipping cost field
//Set the xpsShippingCharge value in invoice , if
//the flat shipping charge is unchecked or shipping cost is less than 1
if(flatShippingCharge == false || shippingCost < 1){
if(xpsShippingCharge == '' || xpsShippingCharge == null){
console.log('no shipping cost');
logme('err@setshippingcost','No shipping cost');
}
else{
invoiceRecord.setValue({
fieldId: 'shippingcost',
value: xpsShippingCharge,
ignoreFieldChange: true
});
}
}
//set custom xpsTrackingNo value to the Tracking Number field
if(xpsTrackingNo == '' || xpsTrackingNo == null){
logme('err@settrackingnumber','No tracking number');
console.log("no tracking no");
}
else{
invoiceRecord.setValue({
fieldId: 'linkedtrackingnumbers',
value: xpsTrackingNo,
ignoreFieldChange: true
});
}
}catch (e) {
logme('err@settingInvoice Values',getError(e));
}
}
else{
console.log('novalue','novalue');
}
} catch (e) {
logme('err@pageInit',e);
}
}
/*******************************************************************************
* return error
*
* @param e
* @returns {String}
*/
function getError(e) {
var stErrMsg = '';
if (e.getDetails != undefined) {
stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
+ e.getStackTrace();
} else {
stErrMsg = '_' + e.toString();
}
return stErrMsg;
}
/*******************************************************************************
* Log these data
*/
function logme(title, details) {
log.debug({
title : title,
details : details
});
}
return {
pageInit: pageInit,
getError: getError,
logme: logme
};
});