Jira Code: BTN-475
This script will source a field from Item Fulfilment to Invoice when it is created. Fields that source from Item Fulfilment are Freight Cost and Freight Price. On the invoice, record values are saved to PJ Shipping Cost and PJ Shiping Price.
define(['N/record'],
/**
* @param {record} record
*/
function(record) {
/**
* before submitting the record, the IF values will be populated to the Invoice
*/
function beforeSubmit(scriptContext) {
try {
if(scriptContext.type == 'create'){
//get the invoice record
var invoiceRecord = scriptContext.newRecord;
var fulfillmentId,freightCost,freightPrice;
//get the sales order from the invoice
var createdFrom = invoiceRecord.getValue({ fieldId : 'createdfrom'});
// if ther is SO, continue
if(createdFrom){
try {
//load the SO
var soRecord = record.load({
type : 'salesorder',
id : createdFrom
});
// get the line counts in related records
var numLines = soRecord.getLineCount({
sublistId: 'links'
});
for (var i = 0; i <= numLines; i++){
// get the related record type from the sublist
var linkType = soRecord.getSublistValue({
sublistId: 'links',
fieldId: 'type',
line: i
});
//if record tYpe is IF, get the IF id
if (linkType == 'Item Fulfillment'){
fulfillmentId = soRecord.getSublistValue({
sublistId: 'links',
fieldId : 'id',
line: i
});
var fulfillRecord = record.load({
type: 'itemfulfillment',
id: fulfillmentId
});
//get the freightCost from IF
var freight_Cost = fulfillRecord.getValue({
fieldId : 'custbody_pacejet_freight_costcurrency'
});
//get the freightPrice from IF
var freight_Price = fulfillRecord.getValue({
fieldId : 'custbody_pacejet_freight_pricecurrency'
});
if(freight_Cost > 0 || freight_Price > 0){
break;
}
}
}
// if fulfillmentId is not null
if (fulfillmentId){
try {
//load the IF record
var fulfillmentRecord = record.load({
type: 'itemfulfillment',
id: fulfillmentId
});
//get the freightCost from IF
freightCost = fulfillmentRecord.getValue({
fieldId : 'custbody_pacejet_freight_costcurrency'
});
//get the freightPrice from IF
freightPrice = fulfillmentRecord.getValue({
fieldId : 'custbody_pacejet_freight_pricecurrency'
});
// if freightCost is not empty, then set the value to the PJ SHIPPING COST in Invoice record
if (freightCost){
invoiceRecord.setValue({
fieldId : 'custbody_rb_pj_shipping_cost',
value : freightCost
});
}
// if freightPrice is not empty, then set the value to the PJ SHIPPING PRICE in Invoice record
if (freightPrice){
invoiceRecord.setValue({
fieldId : 'custbody_rb_pj_shipping_price',
value : freightPrice
});
}
} catch (e) {
logme('err@settingInvoicevalue',getError(e));
}
}
} catch (e) {
logme('err@create',getError(e));
}
}
}
} catch (e) {
logme('err@beforeSubmit',getError(e));
}
}
return {
beforeSubmit: beforeSubmit
};
});
/*******************************************************************************
* 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
});
}