Jira Code: UN-2
When the item quantity is changed the item weight(custom field) will be updated based on the quantity change. When the sales order line item field is updated it will look up that item record and get the weight for the single item. Then it will be calculated for the item quantity and updated in the custom line item field.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/**
* Script Description
* This ClientScript for to change the weight of the item with the quantity change.
*/
/*******************************************************************************
*
*
* NetSuite Name: JJ UN - 315 CS Item Weight Change
*
*
* *****************************************************************************
*
*
* $Author: Jobin & Jismi IT Services LLP $
*
* DESCRIPTION
* To change the weight of the item with the quantity change.
*
* Date Created :14/11/2018
*
* REVISION HISTORY Update:
*
*
*
******************************************************************************/
define(['N/record', 'N/search', 'N/currentRecord', 'N/https', 'N/url'],
function (record, search, currentRecord, https, url) {
var main = {
pageInit: function (scriptContext) {
var records = scriptContext.currentRecord;
var numLines = records.getLineCount({
sublistId: 'item'
});
for (var i = 0; i < numLines; i++) {
var quantity = records.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
var itemid = records.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
var fieldLookUp = search.lookupFields({
type: search.Type.ITEM,
id: itemid,
columns: ['custitem_jj_kg_m']
});
var unitweight = fieldLookUp.custitem_jj_kg_m
var weightset = unitweight * quantity
var lineNum = records.selectLine({
sublistId: 'item',
line: i
});
records.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_weight',
value: parseFloat(weightset).toFixed(2),
ignoreFieldChange: true
});
records.commitLine({
sublistId: 'item'
});
}
},
fieldChanged: function (scriptContext) {
var records = scriptContext.currentRecord;
if ((scriptContext.fieldId == "quantity") && (scriptContext.sublistId == "item")) {
var quantity = records.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
});
var itemid = records.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: scriptContext.line
});
var fieldLookUp = search.lookupFields({
type: search.Type.ITEM,
id: itemid,
columns: ['custitem_jj_kg_m']
});
var unitweight = fieldLookUp.custitem_jj_kg_m
var weightset = unitweight * quantity
records.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_weight',
value: parseFloat(weightset).toFixed(2)
});
}
},
saveRecord: function (scriptContext) {
try {
var numLines = scriptContext.currentRecord.getLineCount({
sublistId: 'item'
});
var totalweight = 0.0;
for (var i = 0; i < numLines; i++) {
var weight = scriptContext.currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_weight',
line: i
});
log.debug("weight", weight);
if ((weight == '') || (weight == null) || (weight == undefined))
weight = 0;
totalweight = parseFloat(totalweight) + parseFloat(weight);
}
log.debug("totalweight", totalweight);
scriptContext.currentRecord.setValue({
fieldId: 'custbody_jj_total_weight',
value: totalweight,
ignoreFieldChange: true
});
} catch (e) {
log.debug("error", e);
}
return true;
}
};
for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
}
function trycatch(myfunction, key) {
return function () {
try {
return myfunction.apply(this, arguments);
} catch (e) {
console.error('e', e);
}
}
};
return main
});