I want to automatically set a value for a custom field in the address sub-record when a new shipping address (address subrecord of a customer record) is registered on the Sales Order form.
In ClientScript's fieldChanged entry point, I created a script like this :
If there are any changes to the list in the "SHIP TO SELECT" field, load the customer record and set the value to a custom field in the address subrecord.
However, when I look at that custom field, the value is not set.
I have confirmed in the log that it is going well up to the script just before that.
How can I solve this?
Here's an excerpt from the script.
----
const editCustomerRecord = record.load({
type: record.Type.CUSTOMER,
id: entityValue
});
const addressCount = editCustomerRecord.getLineCount({
sublistId: 'addressbook'
});
var isChanged = false;
for (var i = 0; i < addressCount; i++) {
var addressId = editCustomerRecord.getSublistValue({
sublistId: 'addressbook',
fieldId: 'id',
line: i
});
// get address subrecord
var addressSubrecord = editCustomerRecord.getSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress',
line: i
});
// if the custom field is null, set value
var nsAddressCd = addressSubrecord.getValue({
fieldId: 'custrecord_nsaddresscd'
});
if(!nsAddressCd){
addressSubrecord.setValue({
fieldId: 'custrecord_nsaddresscd',
value: 'NS' + addressId
});
isChanged = true;
}
}
if(isChanged){
try {
editCustomerRecord.save();
} catch (e) {
log.error({
title: e.name,
details: e.message
});
}
}
----
I'm looking forward to your help.