Does anyone know how to check if a drop down field...
# suitescript
m
Does anyone know how to check if a drop down field (select) has a value? I want to force the user to select a value if one exists. This didn't work:
Copy code
const notcField = currentRecord.getField({
    fieldId: 'custbody_notc'
});

log.debug({title: "NOTC Defintion", details: notcField});

const selectOptions = notcField.selectOptions;
if (!selectOptions || selectOptions.length === 0) {
    if (!natureOfTransactionCode) {
        dialog.alert({
            title: intrastatMessage,
            message: 'Value Required',
        });
        return false;
    }
}
e
Where is the variable "natureOfTransactionCode" coming from?
m
Actually that should be notcField, which is checking if the user has selected a value. That bit is OK. It's only when the drop down is empty in some instances that I need to check for.
I don't think selectOptions is a property of the field object as I see this in the defintion. log.debug({title: "NOTC Definition", details: notcField}); Returns: { id: "custbody_notc", label: "Nature of Transaction Code", type: "select", isMandatory: false, isDisabled: false, isPopup: false, isDisplay: true, isVisible: true, isReadOnly: false }
e
Have you tried getValue() instead of getField()?
If I remember correctly, you can only use getSelectOptions() on a field you created in clientscript.
💯 1
e
You don't read the value of a field by retrieving the
Field
instance. You use Record.getValue(). I believe an empty Select field will return
null
an empty string.
👍 1
I ran this on a Customer record from the console.
cr
is the
N/currentRecord
module, so
get()
returns a
CurrentRecord
instance (similar API to a
Record
instance), and
partner
is an empty dropdown field on the record.
I'm not sure if the empty string is what gets returned in all contexts (e.g. it might be different in a server-side script). I think it would be a safe approach to look for any falsy value when determining if a single Select field is empty.
Copy code
const notc = yourRecord.getValue({ fieldId: 'custbody_notc' })

if (!notc) {
  // the field is empty
}
☝️ 1
m
This is OK once I can determine the drop down has at least one value. if (!notc) { // the field is empty } But my issue is - there will be an instance where the drop down will not have any values at all. In this case, the above will always be true which is not what I want as the user will be asked to select a value when one doesn't exist.
v
Change this
Copy code
const selectOptions = notcField.selectOptions;
To:
Copy code
const selectOptions = notcField.getSelectOptions();
👌 1
e
Some caveats ^ from the reference doc:
You can use this method only in dynamic mode. For additional information about dynamic mode, see CurrentRecord.isDynamic.
If you attempt to get select options on a field that is not a dropdown select field, such as a popup select field or a field that does not exist on the form, null is returned.
A call to this method may return different results for the same field for different roles.
This function returns Type Error if the field is not a supported field for this method.
👍 1
m
Thanks all. I managed to get what I needed. This is working on a client script on the Save button.
s
... or you could just set the field to mandatory
m
I can't as they are not always mandatory.