Jira code : CORP-1
Created column fields in item sublist of the sales order. That field will include the new CBM of each item(CBM of the single item * Quantity) and new weight (weight of single item*Quantity).
We make a client script in which, when an item line is added it will take the quantity and CBM value in each line. On record save, new CBM and new weight set in sublist filed. Also, the total of new CBM is set in the body field Total CBM. The total of new weight is set in the body field total weight.
A mass update script required to update the existing open Sales orders.
User Event Script:-
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/**
* Script Description
* In SO and PO, in after submit function the CBm and weight calculation is done.
.
*
*/
/*******************************************************************************
* CORP DESIGN
* **************************************************************************
*
* Date: 20/05/2019
*
* Author: Jobin & Jismi IT Services LLP
*
*
* REVISION HISTORY
*
* Revision $ 20/05/2019 Maria: 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{
var volume_array = [],
weight_array = [];
var CBM_sum = 0;
var weight_sum = 0;
// To get SO line item count
var currentRec = scriptContext.newRecord;
if(currentRec.type == 'salesorder'){
var currentRec = record.load({
type: record.Type.SALES_ORDER,
id: currentRec.id,
isDynamic: true,
});
}
if(currentRec.type == 'purchaseorder'){
var currentRec = record.load({
type: record.Type.PURCHASE_ORDER,
id: currentRec.id,
isDynamic: true,
});
}
var linecount = currentRec.getLineCount({
sublistId: 'item'
});
for(i=0;i<linecount;i++){
var sublistFieldValue = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
// Not considering the line item if its type is Group start and group end.
if(sublistFieldValue != ('Group')){
if(sublistFieldValue != ('EndGroup')){
var lineNum = currentRec.selectLine({
sublistId: 'item',
line: i
});
// Get the field quantity
var quantity = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'quantity',
line: i
});
// Get the field CBM value
var CBM_value = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'custcol_corpdesign_cbm',
line: i
});
// Get the Item Weight
var item_weight = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'custcol_jj_cd9_item_weight',
line: i
});
// If CBM value, calculates and set New CBM
if(CBM_value){
var new_CBM = quantity*CBM_value;
logme("new_CBM",new_CBM);
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_cbm',
value: new_CBM
});
}else{
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_cbm',
value: 0
});
}
// If weight value, calculates and set New weight
if(item_weight){
var new_weight = quantity*item_weight;
logme("new_weight",new_weight);
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_weight',
value: new_weight
});
}else{
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_weight',
value: 0
});
}
// Get the New Item CBM
var volume = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId : 'custcol_cd9_jj_new_cbm'
});
volume_array.push(volume);
// Get the New Item Weight
var weight = currentRec.getCurrentSublistValue({
sublistId: 'item',
fieldId : 'custcol_cd9_jj_new_weight'
});
weight_array.push(weight);
currentRec.commitLine({
sublistId: 'item'
});
}
}
}
// Calculates Total CBM
for (var i = 0; i < volume_array.length; i++) {
CBM_sum += volume_array[i]
}
// Sum of CBM set in Total CBm field
if(CBM_sum){
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_cbm',
value: CBM_sum,
ignoreFieldChange: true
});
}else{
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_cbm',
value: 0,
ignoreFieldChange: true
});
}
// Calculates Total weight
for (var i = 0; i < weight_array.length; i++) {
weight_sum += weight_array[i]
}
// Sum of CBM set in Total weight field
if(weight_sum){
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_weight',
value: weight_sum,
ignoreFieldChange: true
});
}else{
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_weight',
value: weight_total,
ignoreFieldChange: true
});
}
var recordId = currentRec.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
}catch (e) {
logme("error@afterSubmit", getError(e));
}
}
/*******************************************************************************
* return error
*
* @param e
* @returns
*
* Created on 20-May-2019 by Maria
*/
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
*
* @param title
* @param details
* @returns
*
* Created on 20-May-2019 by Maria
*/
function logme(title, details) {
log.debug({
title : title,
details : details
});
}
return {
//beforeLoad: beforeLoad,
//beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
};
});
Mass Update Script:-
/**
* @NApiVersion 2.x
* @NScriptType MassUpdateScript
* @NModuleScope SameAccount
*/
define(['N/search', 'N/record'],
function(search, record) {
/**
* Definition of Mass Update trigger point.
*
* @param {Object} params
* @param {string} params.type - Record type of the record being processed by the mass update
* @param {number} params.id - ID of the record being processed by the mass update
*
* @since 2016.1
*/
function each(params) {
try{
var SOID = params.id;
logme("SOID",SOID);
var currentRec = record.load({
type: 'salesorder',
id: SOID,
isDynamic: true,
});
//logme("currentRec",currentRec);
var numLines = currentRec.getLineCount({
sublistId: 'item'
});
//logme("numLines",numLines);
for(i=0;i<numLines;i++){
var sublistFieldValue = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
logme("sublistFieldValue",sublistFieldValue);
if(sublistFieldValue != ('EndGroup' || 'Group')){
currentRec.selectLine({
sublistId: 'item',
line: i
});
var item = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'item',
line: i
});
logme("item",item);
var itemdetails = search.lookupFields({
type: search.Type.ITEM,
id: item,
columns: ['custitem_corpdesign_cbm','weight']
});
var volume = itemdetails.custitem_corpdesign_cbm;
var weight = itemdetails.weight;
//logme("volume",volume);
//logme("weight",weight);
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_corpdesign_cbm',
value: volume,
ignoreFieldChange: false
});
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_cd9_item_weight',
value: weight,
ignoreFieldChange: false
});
// Get the field CBM value
var quantity = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'quantity',
line: i
});
//logme("quantity",quantity);
// Get the field CBM value
var CBM_value = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'custcol_corpdesign_cbm',
line: i
});
//logme("CBM_value",CBM_value);
// Get the Item Weight
var item_weight = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'custcol_jj_cd9_item_weight',
line: i
});
//logme("item_weight",item_weight);
if(CBM_value){
var new_CBM = quantity*CBM_value;
logme("new_CBM",new_CBM);
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_cbm',
value: new_CBM,
ignoreFieldChange: false
});
}else{
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_cbm',
value: 0,
ignoreFieldChange: false
});
}
if(item_weight){
var new_weight = quantity*item_weight;
logme("new_weight",new_weight);
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_weight',
value: new_weight,
ignoreFieldChange: false
});
}else{
currentRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_cd9_jj_new_weight',
value: 0,
ignoreFieldChange: false
});
}
// Get the Item Weight
var newvolume = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'custcol_cd9_jj_new_cbm',
line: i
});
// Get the Item Weight
var newweight = currentRec.getSublistValue({
sublistId: 'item',
fieldId : 'custcol_cd9_jj_new_weight',
line: i
});
var total_cbm = currentRec.getValue({
fieldId: 'custbody_jj_cd9_total_cbm'
});
//logme("total_cbm",total_cbm);
var total_weight = currentRec.getValue({
fieldId: 'custbody_jj_cd9_total_weight'
});
//logme("total_weight",total_weight);
//logme("newvolume",newvolume);
//logme("newweight",newweight);
var volume_total = (newvolume + total_cbm);
var weight_total = newweight + total_weight;
//logme("volume_total",volume_total);
//logme("weight_total",weight_total);
if(volume_total){
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_cbm',
value: volume_total,
ignoreFieldChange: true
});
}else{
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_cbm',
value: 0,
ignoreFieldChange: true
});
}
if(weight_total){
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_weight',
value: weight_total,
ignoreFieldChange: true
});
}else{
currentRec.setValue({
fieldId: 'custbody_jj_cd9_total_weight',
value: 0,
ignoreFieldChange: true
});
}
//commit line
currentRec.commitLine({
sublistId: 'item'
});
}
}
var recordId = currentRec.save({
enableSourcing: false,
ignoreMandatoryFields: true
});
}catch (e) {
logme("error@revoveitemupdate", getError(e));
}
}
/*******************************************************************************
* return error
*
* @param e
* @returns
*
* Created on 22-Feb-2019 by Maria
*/
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
*
* @param title
* @param details
* @returns
*
* Created on 22-Feb-2019 by Maria
*/
function logme(title, details) {
log.debug({
title : title,
details : details
});
}
return {
each: each
};
});