We have created a new field on the proposal that sources from our customer list. This will be the general contractor that we will bill. The original customer field will be the end user that the product is installing. The end game would be, when we have an entry in the new customer field, the billaddresslist will show the addresses associated with that new customer. I've created a fieldchanged script that uses that new field and references the customer entered and pulls the addresses associated with that entity.
This is not working, I get an error that it cannot read properties of undefined. I believe I'm referencing the customer incorrectly, I trimmed the name so it only pull the CU id number but that may be the wrong way to do this. I'm new to scripting so any advice or tips would be great :).
I expected this to work but I only get errors.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/log'], function(record, log) {
var cache = {};
function fieldChanged(context) {
if (!context.form) {
return;
}
var customerId = context.currentRecord.getValue({
fieldId: 'my_custom_field'
});
// Check if the customer ID is valid
if (!customerId) {
log.error('Invalid customer ID', customerId);
return;
}
var customerIdArray = customerId.split(" ");
var numericalCustomerId = customerIdArray[0].trim();
// Check if the customer record is already in cache
var customerRecord = cache[numericalCustomerId];
if (!customerRecord) {
// Load the customer record
customerRecord = record.load({
type: record.Type.CUSTOMER,
id: numericalCustomerId,
isDynamic: true
});
// Store the customer record in cache
cache[numericalCustomerId] = customerRecord;
}
var addressCount = customerRecord.getLineCount({
sublistId: 'addressbook'
});
var billaddresslist = context.form.getField({
id: 'billaddresslist'
});
billaddresslist.removeAllOptions();
for (var i = 0; i < addressCount; i++) {
var address = customerRecord.getSublistValue({
sublistId: 'addressbook',
fieldId: 'addressbookaddress',
line: i
});
var option = billaddresslist.addOption({
text: address,
value: i
});
}
}
return {
fieldChanged: fieldChanged,
pageInit: function(context) {
context.form.addFieldChangeListener({
fieldId: 'my_custom_field',
after: fieldChanged
});
}
}; });`