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
};
});
The
removeSelectOptionmethod is only available on Client scripts - not User Event scripts. See the excerpt below from this SuiteAnswers page. Note that it's also only available on fields dynamically added to the user interface - either by a suitelet or abeforeLoaduser event script, and that it's part of theN/currentRecordmodule, which you don't have loaded.You could create a new field to take the place of the 'billaddresslist' field within your
beforeLoadfunction and hide the existing native field. Then populate that custom field with the desired values. You would need a corresponding function on thebeforeSubmituser event to update the value of the native 'billaddresslist' field before the record is saved for it to persist to the database.