Requirement:
Display the sum of amounts (passthrough account number ranges from 1251 to 1271) entered in the expense lines of a Bill in the custom field passthrough/expense on saving the Bills record.
Solution:
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/************************************************************************************************
SWYFT - 184 Automatically Show the amount on Passthrough/Expence Field
*********************************************************************************************
*
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 14/March/2022
*
* Description : Automatically Show the amount on Passthrough/Expence Field-Proposal
*
**********************************************************************************/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
(record, search) => {
const beforeSubmit = (scriptContext) => {
try{
log.debug("started execution")
var billRec = scriptContext.newRecord;
//get the sublist values of the expense sublist
var numLines = billRec.getLineCount({
sublistId: 'expense'
});
let passthroughExpense = billRec.getValue('custbody_jj_passthrough_expense');
var passthroughAmt = 0;
for (let i=0; i<numLines; i++) {
let expenseAcct = billRec.getSublistValue({
fieldId: 'account',
sublistId: 'expense',
line: i
});
log.debug('expenseAcct', expenseAcct)
let expenseAcctName = billRec.getSublistValue({
fieldId: 'account_display',
sublistId: 'expense',
line: i
});
log.debug('expenseAcctName', expenseAcctName)
let expenseAmnt = billRec.getSublistValue({
fieldId: 'amount',
sublistId: 'expense',
line: i
});
log.debug('expenseAmnt', expenseAmnt)
var accNumber = search.lookupFields({
type: search.Type.ACCOUNT,
id: expenseAcct,
columns: ['number']
});
log.debug("number", accNumber.number)
if ((accNumber.number >= 1251) && (accNumber.number <= 1271))
passthroughAmt = passthroughAmt + expenseAmnt;
}
log.debug("passthroughAmt", passthroughAmt);
if (Number(passthroughExpense) !== Number(passthroughAmt)){
billRec.setValue({fieldId: 'custbody_jj_passthrough_expense', value: Number(passthroughAmt)});
}
}catch (e) {
log.debug("Error@beforeSubmit", e)
}
}
return {beforeSubmit}
});