When creating a custom form in Suitelet and attempting to add a selection field, you might encounter the error “addSelectOption is not a function” if you include the isMandatory attribute directly in the field creation object, as shown in the code snippet below:
let gen = form.addField({
id: ‘custpage_gender’,
type: serverWidget.FieldType.SELECT,
label: ‘Gender’,
container: ‘custpage_primary’
}).isMandatory = true;
gen.addSelectOption({
value: ”,
text: ”,
});
This error occurs because the chaining of the isMandatory property directly after the addField method interferes with the proper initialization of the field object. To rectify this error, you should set the isMandatory attribute on a separate line, ensuring that the field object is fully initialized before modifying its properties. The corrected code would look like this:
let gen = form.addField({
id: ‘custpage_gender’,
type: serverWidget.FieldType.SELECT,
label: ‘Gender’,
container: ‘custpage_primary’
});
gen.isMandatory = true;
gen.addSelectOption({
value: ”,
text: ”,
});
By setting the isMandatory value separately, you allow the gen field object to be properly created and initialized, thus enabling the subsequent method addSelectOption to be called without any issues.