I want to add a little asterisk next to a field th...
# suitescript
w
I want to add a little asterisk next to a field through client script to indicate that it is mandatory .This works well on custom fields but not on standard ones.
curRec.getField({ fieldId: 'phone' }).isMandatory = true
. I know it won't validate them on save, but I have a check in saveRecord if it is mandatory and throw an error it is not filled out. Running the code in the console works but not through the client script entry points. Anyone has any solutions? I would prefer not to add the asterisk through DOM manipulation. And it would be good if the field could hold it's isMandatory property itself. According to this SA, isMandatory is read-only. But the help center does not state the same. And obviously it is not read-only on custom fields.
z
custom form? Then you can set mandatory status for all fields you want.. Custom form cpuld be preferred for particular user or all users
w
It is a custom form, but I only want some fields to be mandatory depending on what the user inputs in other fields. And also, I want to make the taxidnum and vatregnumber mandatory when they are visible. They are hidden depending on which nexus the primary subsidiary is in. If I make them both mandatory on the form, I'll get an error on save as one of them is blank and not displayed.
z
it is over my knowledge limits 😕 If I understood correctly, you want to change status dynamicly, depending on the values of other fields on the form...It sounds like jQuery zone 😁... sorry, I have no strong experience with client side and fromt-end programming
j
Seems like either way would work, but...the way below does work for me in client script. Didn't try it all in one line, though.
Copy code
var phone = curRec.getField('phone');
phone.isMandatory = true;
w
I just condensed it for pasting. I actually do that in a way.
Copy code
FIELDS_TO_VALIDATE_FOR_MANDATORY.forEach(fieldId => {
    const field = curRec.getField({ fieldId })
    field.isMandatory = field.isDisplay
})
I've stepped through the code in the chrome debugger when it is executed and it looks good there, but nothing is happening.
n
You can set fields to mandatory on beforeload in a user event but the issue will likely be that your matching criteria will not be available since I'm going to guess it will be based on other field data? If it's based off maybe a lookup, role or some value on the employee record this could be a solution for you though. @Watz
w
Yup, it is dynamically based on another field.
I ended up setting up my own variable-object for tracking the mandatory status and based on that added the star using a copy of Netsuites own utility function setFieldLabelRequired()
Copy code
const SPECIAL_FIELDS_TO_VALIDATE_FOR_MANDATORY = {
    [VENDOR.FIELDS.TAX_ID_NUM]: false,
    [VENDOR.FIELDS.VAT_REG_NR]: false,
    [VENDOR.FIELDS.DEFAULT_ADDRESS]: false,
}
Copy code
const setFieldLabelRequired = (fldName: string, required: boolean): void => {
    if (fldName) {
        fldName = fldName.replace('inpt_', '')
        fldName = fldName.replace('hddn_', '')
        fldName = fldName.replace('_fs', '')

        const label = document.getElementById(fldName + '_fs_lbl')
        if (label) {
            // Only add the asterisk to the first child label
            if ((label.parentNode && label.parentNode.firstChild !== label) || (label.className && label.className.indexOf('uir-label-no-required-flag') !== -1)) {
                return
            }

            const labels = label.getElementsByTagName('label')
            let asteriskLabel
            for (let i = 0; i < labels.length; i++) {
                if (labels[i].className === 'uir-required-icon')
                {
                    asteriskLabel = labels[i]
                    break
                }
            }

            if (required && !asteriskLabel) {
                asteriskLabel = document.createElement('label')
                asteriskLabel.className = 'uir-required-icon'
                asteriskLabel.textContent = '*'
                label.appendChild(asteriskLabel)
            }
            else if (!required && asteriskLabel) {
                label.removeChild(asteriskLabel)
            }
        }
    }
}
Copy code
Object.keys(SPECIAL_FIELDS_TO_VALIDATE_FOR_MANDATORY)
        .forEach(fieldId => setFieldLabelRequired(fieldId, false))
    Object.keys(SPECIAL_FIELDS_TO_VALIDATE_FOR_MANDATORY)
        .filter(fieldId => SPECIAL_FIELDS_TO_VALIDATE_FOR_MANDATORY[fieldId])
        .forEach(fieldId => setFieldLabelRequired(fieldId, true))