What It Does
The setMandatory method dynamically controls whether a form field is required on the client-side. When you set a field to mandatory (true), ServiceNow adds a red asterisk next to the field label and prevents form submission if the field is empty. Setting it to optional (false) removes the asterisk and allows the form to be submitted with an empty value.
Internally, ServiceNow updates the field's client-side properties and modifies the form's validation behavior. The platform adds or removes the mandatory CSS class from the field's label element and updates the form's validation registry. This change is immediate and visible to the user without requiring a page refresh or server round-trip.
The method returns void and provides no feedback about success or failure. If you specify a field name that doesn't exist on the current form, the method fails silently without throwing an error or logging a warning. This can make debugging field name typos challenging, especially when the method call appears to work but has no visible effect.
The mandatory state change only persists for the current browser session. If the user refreshes the page or navigates away and returns, the field reverts to its original mandatory state as defined in the dictionary or UI policy. This client-only behavior means setMandatory cannot override server-side validation rules — a field that's mandatory at the dictionary level will still be enforced on the server even if you set it to optional on the client.
This method works closely with isMandatory, which checks the current mandatory state, and clearValue or setValue which modify field values. When building dynamic forms, you'll typically use these methods together to create conditional validation based on user selections or field values.
When to Use This
Use setMandatory when you need conditional field requirements based on user input or dynamic form behavior. Common scenarios include making fields required when a specific category is selected, requiring approval justification when certain conditions are met, or creating stepped forms where different sections become required as the user progresses. This method is perfect for improving user experience by showing requirements contextually rather than overwhelming users with all mandatory fields upfront.
For permanent field requirements that apply to all records, use dictionary mandatory settings or data policies instead of client scripts. If you need server-side enforcement of dynamic requirements, implement the logic in a Business Rule rather than relying solely on setMandatory. For complex conditional requirements that involve multiple fields or business logic, consider using UI Policies which provide a more maintainable declarative approach than multiple client scripts.
Return Value
The method returns void (undefined) in all cases, whether the operation succeeds or fails. There is no return value to check for success, and no exception is thrown for invalid field names or parameters. The only way to verify the method worked is to visually inspect the form or use g_form.isMandatory(fieldName) to check the resulting state.
Since there's no failure indication, always double-check field names for typos and test thoroughly in your target environment. The silent failure behavior means a misspelled field name will result in no visual change to the form, which can be mistaken for the method not working properly when the issue is actually an incorrect parameter.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- No server-side Business Rules, workflows, or notifications are triggered by this method since it only affects client-side display and validation
- No database writes occur — the mandatory state change exists only in browser memory until the page is refreshed or the user navigates away
- Executes immediately with no network latency — performance impact is negligible since it only manipulates DOM elements and client-side validation arrays
- Changes are immediately visible to the user and affect form submission validation, but do not override server-side mandatory field validation configured in the dictionary
- Multiple calls to the same field will overwrite previous settings — the last call determines the final mandatory state
- UI Policies that run after your client script can override the mandatory state set by this method, creating potential conflicts in complex forms
- Form submission will be blocked if any fields set to mandatory via this method are empty, displaying the standard "The following mandatory fields are not filled in" error message