I have a script that "should"
- Get the customer ID from a created customer field on my form
- Load the customer record
- Remove options that are already in the 'billaddresslist'
- Search for addresses associated with my selected customer
- add each address as a select option in the 'billaddresslist' field.
I continue to get an error when reloading my form, "Cannot find function removeSelectOption in object Field".
I tried this script below, any suggestions or advice?
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/search'], function(record, search) {
function beforeLoad(context) {
var currentRecord = context.newRecord;
// Get the customer ID from the "customer" field on the form
var customerId = currentRecord.getValue({
fieldId: 'my_custom_field'
});
if (customerId) {
// Load the customer record
var customerRecord = record.load({
type: record.Type.CUSTOMER,
id: customerId,
isDynamic: true
});
// Remove any existing options in the "billaddresslist" field
currentRecord.getField({
fieldId: 'billaddresslist'
}).removeSelectOption({
value: null,
filter: null,
operator: null
});
// Search for all addresses associated with the customer
var addressSearch = search.create({
type: 'address',
filters: [
['entity', 'is', customerId],
],
columns: [
'internalid',
'addressbookaddress'
]
});
// Add each address as a select option in the "billaddresslist" field
addressSearch.run().each(function(result) {
currentRecord.getField({
fieldId: 'billaddresslist'
}).addSelectOption({
value: result.getValue('internalid'),
text: result.getValue('addressbookaddress')
});
return true;
});
}
}
return {
beforeLoad: beforeLoad
};
});