Hi everyone, I'm having an issue with setting a sublist value in a client script attached to a Sales Order. The script runs on the fieldChanged event and it executes on 'item' field update in item sublist and 'shipaddress' update on the main form. It works as such:
- When a user adds/updates an item on the item sublist the code checks the value of the 'shipcountry' field. The value of 'taxcode' on the sublist line is then set based on the value of 'shipcountry' (e.g. if US then the appropriate US taxcode is set, if CA then appropriate Canadian taxcode is set, etc.)
- When a user updates the 'shipaddress' field the code retrieves the value of 'shipcountry' and iterates through all lines in the sublist and sets the value of 'taxcode' based on the updated value for 'shipcountry'.
Here's the code:
function fieldChanged(context) {
// only looking at 'taxcode' here to check it's initial value
if (context.fieldId == 'item' || context.fieldId == 'shipaddress' || context.fieldId == 'taxcode') {
if (context.fieldId == 'taxcode') {
var code = context.currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'taxcode',
line: 0
});
log.debug('line 0 taxcode value', code);
}
}
// only concerned with 'item' and 'shipaddress' updates
if (context.fieldId != 'item' && context.fieldId != 'shipaddress') {
return;
}
log.debug('valid fieldId, executing...', context.fieldId);
var currentRecord = context.currentRecord;
var shipcountry = currentRecord.getValue({
fieldId: 'shipcountry'
});
log.debug('Shipping country code', shipcountry);
var shippingTaxCode = 540;
if (shipcountry == 'US') {
shippingTaxCode = 3241;
} else if (shipcountry == 'CA') {
shippingTaxCode = 3242;
}
currentRecord.setValue({
fieldId: 'shippingtaxcode',
value: shippingTaxCode
});
log.debug('Setting shiptaxitem', shippingTaxCode);
// 'item' field in sublist is updated
if (context.fieldId == 'item') {
log.debug('executing against item');
var sublistName = context.sublistId;
if (sublistName == 'item') {
if (currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'taxcode'
}) !== shippingTaxCode) {
currentRecord.setCurrentSublistValue({
sublistId: sublistName,
fieldId: 'taxcode',
value: shippingTaxCode
});
currentRecord.commitLine({
sublistId: 'item'
});
}
}
} else if (context.fieldId == 'shipaddress') { // 'shipaddress' field is updated in form
log.debug('executing against shipaddress');
var sublistName = 'item';
var lines = currentRecord.getLineCount({
sublistId: sublistName
});
if (lines > 0) {
for (var i = 0; i < lines; i++) {
log.debug('sync sublist line ' + i + ' with new taxcode, ' + shippingTaxCode);
var line = currentRecord.selectLine({
sublistId: sublistName,
line: i
});
log.debug('current line', line);
line.setCurrentSublistValue({
sublistId: sublistName,
fieldId: 'taxcode',
value: shippingTaxCode,
ignoreFieldChange: true
});
line.commitLine({
sublistId: sublistName
});
currval = line.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'taxcode'
});
log.debug('taxcode value after line commit', currval);
}
}
}
return true;
}
Scenario 1 works great, scenario 2 does not work at all. I believe I'm doing everything correctly but I am not able to set the value of 'taxcode' in the sublist lines when it is driven by an update from 'shipaddress'. I've logged the value of 'taxcode' after I've updated the field and it is indeed being set. One thing that I found interesting is that when fieldChanged is executed when 'taxcode' itself is updated the value of 'taxcode' is the old value and not the new value that was literally just checked as being updated. So, I'm really confused here as to why this isn't being updated. The only thing I can think of is that I'm not doing the update correctly, but I've looked over this code numerous times and I can't see where I'm doing something incorrectly.
I've also tried moving the script to be last in the execution order to rule out another script updating 'taxcode' but it still doesn't get set correctly. It might be specific to our environment, but is there anything in general that I'm missing here? I have read that there was a defect at one point where taxcode was not being set via REST request, but I don't believe that would apply to us. I also found a number of documents in SuiteAnswers targeting how to update taxcode in line items, but all those examples show it being used in saveRecord or beforeSubmit and not in fieldChanged.
Something else I want to mention is that I have a UE script that fires on beforeSubmit that updates the field with no issues, so this makes me think that I'm doing something incorrect in my client script.
function beforeSubmit(context) {
var currentRecord = context.newRecord;
var stc = currentRecord.getValue({
fieldId: 'shippingtaxcode'
});
log.debug('beforeSubmit: shippingtaxcode', stc);
var lineCount = currentRecord.getLineCount({
sublistId: 'item'
});
for (var i = 0; i < lineCount; i++) {
var taxCode = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'taxcode',
line: i
});
if (taxCode !== stc) {
currentRecord.setSublistValue({
sublistId: 'item',
fieldId: 'taxcode',
value: stc,
line: i
});
}
}
}
Thanks for any and all help!