I am developing a ClientScript for Sales Orders and Quotes. I added a dropdown select transaction line field to select a commission value based on the profit margin of the line item.
For each line item, get the amount and costestimate and calculate gross profit. If the gross profit is within a certain range, set this value on the commission select.
Below is the code I wrote up:
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord'], (currentRecord)=>{
const CommLoad = (context)=>{
var currRecord = context.currentRecord,
lines = currRecord.getLineCount({ sublistId: 'item' });
for(let i=0; i<lines; i++){
const lineNum = currRecord.selectLine({ sublistId: 'item', line: i });
var lAmnt = currRecord.getCurrentSublistValue({ sublistId: 'item', line: i, fieldId: 'amount'});
var lCost = currRecord.getCurrentSublistValue({ sublistId: 'item', line: i, fieldId: 'costestimate'});
var GP = Math.trunc(((lAmnt-lCost)/lAmnt)*100); GP = GP.toFixed(2);
//console.log('line: '+i,'Amount: '+lAmnt,'Cost: '+lCost,'Gross Profit: '+GP);
if (GP < 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 1
});
}
if (GP >= 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 2
});
}
if (GP >= 15.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 3
});
}
if (GP >= 20.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 4
});
}
if (GP >= 25.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 5
});
}
if (GP >= 30.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 6
});
}
if (GP >= 35.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 7
});
}
if (GP >= 40.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 8
});
}
if (GP >= 45.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 9
});
}
else {
return true;
}
} //end for
}
const CommValidate = (context) =>{
var currRecord = context.currentRecord,
lines = currRecord.getLineCount({ sublistId: 'item' }),
lineNum = currRecord.getCurrentSublistValue({ sublistId: 'item', fieldId: 'line' }),
lAmnt = currRecord.getCurrentSublistValue({ sublistId: 'item', line: lineNum, fieldId: 'amount'}),
lCost = currRecord.getCurrentSublistValue({ sublistId: 'item', line: lineNum, fieldId: 'costestimate'}),
GP = Math.trunc(((lAmnt-lCost)/lAmnt)*100); GP = GP.toFixed(2);
//console.log('line: '+lineNum,'Amount: '+lAmnt,'Cost: '+lCost,'Gross Profit: '+GP);
if (GP < 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 1
});
}
if (GP >= 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 2
});
}
if (GP >= 15.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 3
});
}
if (GP >= 20.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 4
});
}
if (GP >= 25.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 5
});
}
if (GP >= 30.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 6
});
}
if (GP >= 35.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 7
});
}
if (GP >= 40.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 8
});
}
if (GP >= 45.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 9
});
}
}
const CommFChange = (context) =>{
}
return {
pageInit: CommLoad,
validateLine: CommValidate,
fieldChanged: CommFChange
};
});
The script works perfectly on pageInit, when the line items load up, any without a commission value, will be assigned one.
I am now running into an issue on validateLine, I can update the rate or even add a new item and the variables will calculate and assign a commission value, but I am unable to leave the line item, I can keep pressing "add" or "ok" and it will not take me anywhere.
I did try to add a commitLine to the statements or even outside the statement, and I got this error: RangeError Maximum call stack size exceeded
It looked like at the end of my validateline function, I needed to add a return true.
After I added that, I was able to bounce off onto items and move around. Works like a charm. Thanks again Krypton!
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord','N/record'], (currentRecord,record)=>{
const CommLoad = (context)=>{
var currRecord = context.currentRecord,
lines = currRecord.getLineCount({ sublistId: 'item' });
for(let i=0; i<lines; i++){
const lineNum = currRecord.selectLine({ sublistId: 'item', line: i });
var lAmnt = currRecord.getCurrentSublistValue({ sublistId: 'item', line: i, fieldId: 'amount'});
var lCost = currRecord.getCurrentSublistValue({ sublistId: 'item', line: i, fieldId: 'costestimate'});
var GP = Math.trunc(((lAmnt-lCost)/lAmnt)*100); GP = GP.toFixed(2);
//console.log('line: '+i,'Amount: '+lAmnt,'Cost: '+lCost,'Gross Profit: '+GP);
if (GP < 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 1
});
}
if (GP >= 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 2
});
}
if (GP >= 15.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 3
});
}
if (GP >= 20.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 4
});
}
if (GP >= 25.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 5
});
}
if (GP >= 30.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 6
});
}
if (GP >= 35.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 7
});
}
if (GP >= 40.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 8
});
}
if (GP >= 45.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: i,
fieldId: 'custcol3',
value: 9
});
}
} //end for
}
const CommValidate = (context) =>{
var currRecord = context.currentRecord,
lineNum = currRecord.getCurrentSublistValue({ sublistId: 'item', fieldId: 'line' }),
lAmnt = currRecord.getCurrentSublistValue({ sublistId: 'item', line: lineNum, fieldId: 'amount'}),
lCost = currRecord.getCurrentSublistValue({ sublistId: 'item', line: lineNum, fieldId: 'costestimate'}),
GP = Math.trunc(((lAmnt-lCost)/lAmnt)*100); GP = GP.toFixed(2);
//console.log('line: '+lineNum,'Amount: '+lAmnt,'Cost: '+lCost,'Gross Profit: '+GP);
if (GP < 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 1
});
}
if (GP >= 10.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 2
});
}
if (GP >= 15.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 3
});
}
if (GP >= 20.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 4
});
}
if (GP >= 25.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 5
});
}
if (GP >= 30.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 6
});
}
if (GP >= 35.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 7
});
}
if (GP >= 40.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 8
});
}
if (GP >= 45.00) {
currRecord.setCurrentSublistValue({
sublistId: 'item',
line: lineNum,
fieldId: 'custcol3',
value: 9
});
}
return true;
}
const CommFChange = (context) =>{
}
return {
pageInit: CommLoad,
validateLine: CommValidate,
fieldChanged: CommFChange
};
});