When an item is added in a sales order, purchase order, and estimate record, item weight and M3 should be calculated and set in the column fields. Also, the total weight and M3 should be set in the body fields. The script is deployed to Sales order, purchase order, quote/estimate records, and is applicable only for inventory and kit/package items.
The script is deployed to sales orders, purchase orders, and Quote/Estimate records. The item level calculation occurs in the UI context (Client Script) and CSV context(User Event script).
ClientScript
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/****************************************************************************
* VERVE Fitness
* VF-3 Item field value population and the Total value calculation
* **************************************************************************
* Date: 02/04/2020
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : To populate the values to the lines column fields(Total Weight and M3) when adding an item. Also, it will update the body field by calculating the total of values from all item lines.
* Deployed to SO, PO, Quote/Estimate
* Date created : 02 April 2020
*
* REVISION HISTORY
*
* Revision 1.0 02/04/2020 md: Create
*
****************************************************************************/
define(['N/currentRecord', 'N/record', 'N/search'],
function(currentRecord, record, search) {
function pageInit(scriptContext) {
try {
var currentRec = scriptContext.currentRecord;
var CreatedFrom = currentRec.getValue({
fieldId: 'createdfrom'
});
//if created from
if (CreatedFrom) {
var ItemlineCount = currentRec.getLineCount({
sublistId: 'item'
});
console.log("ItemlineCount", ItemlineCount);
for (var i = 0; i < ItemlineCount; i++) {
var lineNum = currentRec.selectLine({
sublistId: 'item',
line: i
});
var ItemType = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'itemtype'
});
//Check item type is Inventory or Kit/package item
if ((ItemType == "Kit" || ItemType == "InvtPart")) {
var ItemId = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
//Fetch data from item record.
if (ItemType == "InvtPart") {
var ItemValues = search.lookupFields({
type: search.Type.INVENTORY_ITEM,
id: ItemId,
columns: ['weight', 'custitem_ns_item_m3']
});
//Weight and M3 value
var ItemWeight = ItemValues.weight;
var ItemM3 = ItemValues.custitem_ns_item_m3;
}
//Fetch data from item record.
if (ItemType == "Kit") {
var ItemValues = search.lookupFields({
type: search.Type.KIT_ITEM,
id: ItemId,
columns: ['weight', 'custitem_ns_item_m3']
});
//Weight and M3 value
var ItemWeight = ItemValues.weight;
var ItemM3 = ItemValues.custitem_ns_item_m3;
}
var ItemQuantity = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
});
var LineWeight = ItemWeight * ItemQuantity;
if (LineWeight) {
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_weight_vf_1',
value: parseFloat(LineWeight),
ignoreFieldChange: false
});
}
if (ItemM3) {
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_m3_vf_1',
value: parseFloat(ItemM3),
ignoreFieldChange: false
});
}
currentRec.commitLine({
sublistId: 'item'
});
}
}
}
} catch (e) {
console.log("ERROR_ON_pageInit", getError(e));
}
}
function fieldChanged(scriptContext) {
try {
//Trigger fieldchange and update item weight column.
if (scriptContext.fieldId == "quantity") {
setWeightM3(scriptContext);
}
} catch (e) {
console.log("ERROR_ON_FIELDCHANGED", getError(e));
}
}
function postSourcing(scriptContext) {
try {
//Trigger item add and update item weight, M3 column fields
if (scriptContext.fieldId == "item") {
setWeightM3(scriptContext);
}
} catch (e) {
console.log("ERROR_ON_POSTSOURCING", getError(e));
}
}
/*************************************************
* Created on 03 April 2020
*
* This function will trigger sublist change update Total Weight and Total M3(body fields)
* on commit and remove action.
*
*************************************************/
function sublistChanged(scriptContext){
try {
// On commit action
var currentRec = scriptContext.currentRecord;
if (scriptContext.operation == "commit"){
var ItemlineCount = currentRec.getLineCount({
sublistId: 'item'
});
console.log("ItemlineCount", ItemlineCount);
var NewTotalItemWeight = 0;
var NewTotalItemM3 = 0;
for (var j = 0; j < ItemlineCount; j++) {
var ItemType = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: j
});
if ((scriptContext.sublistId == "item") && (ItemType == "Kit" || ItemType == "InvtPart")) {
var LineItemWeight = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_weight_vf_1',
line: j
});
if (LineItemWeight == null || LineItemWeight == undefined || LineItemWeight == "null" || LineItemWeight == "undefined" || LineItemWeight == "" || LineItemWeight == " ") {
LineItemWeight = 0;
}
NewTotalItemWeight += LineItemWeight;
var LineIteM3 = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_m3_vf_1',
line: j
});
if (LineIteM3 == null || LineIteM3 == undefined || LineIteM3 == "null" || LineIteM3 == "undefined" || LineIteM3 == "" || LineIteM3 == " ") {
LineIteM3 = 0;
}
NewTotalItemM3 += parseFloat(LineIteM3);
}
}
//Set item line weight
if (NewTotalItemWeight) {
currentRec.setValue({
fieldId: 'custbody_jj_total_item_weight_vf_1',
value: parseFloat(NewTotalItemWeight)
});
}
//Set Item M3
if (NewTotalItemM3) {
currentRec.setValue({
fieldId: 'custbody_jj_total_m3_vf_1',
value: (NewTotalItemM3).toFixed(4)
});
}
}
if (scriptContext.operation == "remove") {
removeWeightM3(scriptContext);
}
} catch (e) {
console.log("ERROR_ON_SUBLISTCHANGED", getError(e));
}
}
/*************************************************
* Created on 03 April 2020
*
* Calls this function from field change and postsourcing.
* Will update item columns Item weight and M3 by fetching data from item record.
*
*************************************************/
function setWeightM3(scriptContext) {
try {
var currentRec = scriptContext.currentRecord;
var ItemType = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'itemtype'
});
//Check item type is inventory or kit/package items
if ((scriptContext.sublistId == "item") && (ItemType == "Kit" || ItemType == "InvtPart")) {
var ItemId = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
//Fetch data from item record.
if (ItemType == "InvtPart") {
var ItemValues = search.lookupFields({
type: search.Type.INVENTORY_ITEM,
id: ItemId,
columns: ['weight', 'custitem_ns_item_m3']
});
//Weight and M3 value
var ItemWeight = ItemValues.weight;
var ItemM3 = ItemValues.custitem_ns_item_m3;
}
//Fetch data from item record.
if (ItemType == "Kit") {
var ItemValues = search.lookupFields({
type: search.Type.KIT_ITEM,
id: ItemId,
columns: ['weight', 'custitem_ns_item_m3']
});
//Weight and M3 value
var ItemWeight = ItemValues.weight;
var ItemM3 = ItemValues.custitem_ns_item_m3;
}
var ItemQuantity = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
});
var LineWeight = ItemWeight * ItemQuantity;
//Set line item weight
if (LineWeight) {
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_weight_vf_1',
value: parseFloat(LineWeight),
ignoreFieldChange: true
});
}
//set line item M3
if (ItemM3) {
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_m3_vf_1',
value: parseFloat(ItemM3),
ignoreFieldChange: true
});
}
}
} catch (e) {
console.log("ERROR_ON_SETWEIGHTM3", getError(e));
}
}
function removeWeightM3(scriptContext) {
try {
var currentRec = scriptContext.currentRecord;
var ItemType = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'itemtype'
});
//Check item type is Inventory or Kit/package item
if ((scriptContext.sublistId == "item") && (ItemType == "Kit" || ItemType == "InvtPart")) {
var TotalItemWeight = currentRec.getValue({
fieldId: 'custbody_jj_total_item_weight_vf_1'
});
var LineItemWeight = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_weight_vf_1'
});
var NewTotalItemWeight = TotalItemWeight - LineItemWeight;
var TotalM3 = currentRec.getValue({
fieldId: 'custbody_jj_total_m3_vf_1'
});
var LineIteM3 = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_m3_vf_1'
});
if (TotalM3 == null || TotalM3 == undefined || TotalM3 == "null" || TotalM3 == "undefined" || TotalM3 == "" || TotalM3 == " ") {
TotalM3 = 0;
}
if (LineIteM3 == null || LineIteM3 == undefined || LineIteM3 == "null" || LineIteM3 == "undefined" || LineIteM3 == "" || LineIteM3 == " ") {
LineIteM3 = 0;
}
var NewTotalItemM3 = parseFloat(TotalM3) - parseFloat(LineIteM3);
currentRec.setValue({
fieldId: 'custbody_jj_total_item_weight_vf_1',
value: parseFloat(NewTotalItemWeight)
});
currentRec.setValue({
fieldId: 'custbody_jj_total_m3_vf_1',
value: (parseFloat(NewTotalItemM3)).toFixed(4)
});
}
} catch (e) {
console.log("ERROR_ON_removeWeightM3", getError(e));
}
}
/*************************************************
* Created on 03 April 2020
*
* return error
*************************************************/
function getError(e) {
var stErrMsg = '';
if (e.getDetails != undefined) {
stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>' +
e.getStackTrace();
} else {
stErrMsg = '_' + e.toString();
}
return stErrMsg;
}
return {
pageInit: pageInit,
fieldChanged: fieldChanged,
postSourcing: postSourcing,
sublistChanged: sublistChanged
};
});
UserEventScript
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/****************************************************************************
* VERVE Fitness
* VF-6 Item field value population and the Total value calculation
* **************************************************************************
* Date: 23/04/2020
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : To populate the values to the lines column fields(Total Weight and M3). Also, it will update the body field(Total weight and Total M3) by calculating the total of values from all item lines.
* Deployed to Sales Order, Purscchase Order, Quote/Estimate record.
* Date created : 23 April 2020
*
* REVISION HISTORY
*
* Revision 1.0 23/04/2020 md: Create
*
****************************************************************************/
define(['N/record', 'N/search'],
function(record, search) {
/**
* 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 {
//Work on create context
if (scriptContext.type == 'create') {
var NewTotalItemWeight = 0;
var NewTotalItemM3 = 0;
var RecordId = scriptContext.newRecord.id;
//Loads sales order
if (scriptContext.newRecord.type == 'salesorder') {
var currentRec = record.load({
type: record.Type.SALES_ORDER,
id: RecordId
});
}
//Loads estimate
if (scriptContext.newRecord.type == 'estimate') {
var currentRec = record.load({
type: record.Type.ESTIMATE,
id: RecordId
});
}
//Loads purchase order
if (scriptContext.newRecord.type == 'purchaseorder') {
var currentRec = record.load({
type: record.Type.PURCHASE_ORDER,
id: RecordId
});
}
var itemLineCount = currentRec.getLineCount({
sublistId: 'item'
});
for (var i = 0; i < itemLineCount; i++) {
var ItemType = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
if ((ItemType == "Kit" || ItemType == "InvtPart")) {
var ItemId = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
//Fetch data from item record.
if (ItemType == "InvtPart") {
var ItemValues = search.lookupFields({
type: search.Type.INVENTORY_ITEM,
id: ItemId,
columns: ['weight', 'custitem_ns_item_m3']
});
//Weight and M3 value
var ItemWeight = ItemValues.weight;
var ItemM3 = ItemValues.custitem_ns_item_m3;
}
//Fetch data from item record.
if (ItemType == "Kit") {
var ItemValues = search.lookupFields({
type: search.Type.KIT_ITEM,
id: ItemId,
columns: ['weight', 'custitem_ns_item_m3']
});
//Weight and M3 value
var ItemWeight = ItemValues.weight;
var ItemM3 = ItemValues.custitem_ns_item_m3;
}
var ItemQuantity = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
var LineItemWeight = ItemWeight * ItemQuantity;
var LineIteM3 = ItemM3 * ItemQuantity;
if (LineItemWeight) {
currentRec.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_weight_vf_1',
line: i,
value: parseFloat(LineItemWeight)
});
}
if (LineItemWeight == null || LineItemWeight == undefined || LineItemWeight == "null" || LineItemWeight == "undefined" || LineItemWeight == "" || LineItemWeight == " ") {
LineItemWeight = 0;
}
NewTotalItemWeight += LineItemWeight;
if (LineIteM3) {
currentRec.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_item_m3_vf_1',
line: i,
value: (parseFloat(LineIteM3)).toFixed(4)
});
}
if (LineIteM3 == null || LineIteM3 == undefined || LineIteM3 == "null" || LineIteM3 == "undefined" || LineIteM3 == "" || LineIteM3 == " ") {
LineIteM3 = 0;
}
NewTotalItemM3 += parseFloat(LineIteM3);
}
}
//Set item line weight
if (NewTotalItemWeight) {
currentRec.setValue({
fieldId: 'custbody_jj_total_item_weight_vf_1',
value: parseFloat(NewTotalItemWeight)
});
}
//Set Item M3
if (NewTotalItemM3) {
currentRec.setValue({
fieldId: 'custbody_jj_total_m3_vf_1',
value: (NewTotalItemM3).toFixed(4)
});
}
currentRec.save();
}
} catch (e) {
log.debug("ERROR_ON_AFTERSUBMIT", getError(e));
}
}
/*************************************************
* Created on 23 April 2020
*
* return error
*************************************************/
function getError(e) {
var stErrMsg = '';
if (e.getDetails != undefined) {
stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>' +
e.getStackTrace();
} else {
stErrMsg = '_' + e.toString();
}
return stErrMsg;
}
return {
afterSubmit: afterSubmit
};
});