UI policies handle most conditional field requirements, but they break down when you need complex logic or custom conditions. This guide shows you how to use client scripts to make fields mandatory based on business rules that UI policies can't express.
When UI policies aren't enough
UI policies work great for simple conditions — show this field when that field equals X — but they fail when you need multiple field evaluations, date comparisons, or custom JavaScript logic. When someone fills out a form and your requirement is 'if priority is high AND assignment group contains managers AND created date is after business hours, then require justification', you're stuck. Form designers try workarounds with multiple UI policies and get frustrated when ServiceNow won't let them build complex AND/OR conditions.
How client scripts handle mandatory fields
A client script with g_form.setMandatory() gives you full JavaScript control over when fields are required. You write an onChange script that evaluates your conditions and sets mandatory true or false accordingly. The script runs immediately when the triggering field changes, so users see the red asterisk appear or disappear in real time. Start with a simple two-field dependency, then add complexity as needed. The key pieces are: the triggering field in your script's 'Applies on' field, the onChange function that evaluates conditions, and the g_form.setMandatory() calls that enforce the requirement.
Beyond basic mandatory logic
Once you have working conditional requirements, the improvements that matter are adding server-side validation (client scripts can be bypassed), handling edge cases like bulk updates and web services, and optimizing performance when you're evaluating many fields. Consider data policies for server-side enforcement and business rules for validation that web service calls respect. Well-designed mandatory field logic works the same whether someone fills out the form manually or updates the record through integration.
Before you start
- •admin role or client_script_admin role
Sourdough: ServiceNow Monitoring and Analytics
A Chrome extension for ServiceNow Admins and Developers with essential tools, analytics, graphs and monitoring features.
Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.
Step by step
Create the client script
Navigate to System Definition > Client Scripts and click New. Set Name to something descriptive like 'Make category mandatory when priority is high'. Set Table to your target table. Set Type to 'onChange' — this fires when field values change. In the Applies on field, enter the name of the field that triggers the mandatory logic, not the field you're making mandatory.
If multiple fields should trigger the logic, create separate client scripts for each trigger field that call the same evaluation function.
Write the onChange function
In the Script field, write a function that gets the triggering field's value and evaluates your condition. Use g_form.getValue('field_name') to read field values and g_form.setMandatory('target_field', true) to require the field when your condition is met. Always include the false case with g_form.setMandatory('target_field', false) to clear the requirement when conditions change.
Use g_form.getValue() instead of direct field access — it handles choice fields and reference fields correctly.
Test the mandatory behavior
Save the client script and open a record on your target table. Change the triggering field value to meet your condition and verify the target field shows a red asterisk and 'mandatory' indicator. Change it back and confirm the mandatory requirement disappears. Try submitting the form both ways to ensure the validation fires correctly.
Handle form load scenarios
Create a second client script with Type 'onLoad' that runs the same mandatory logic when the form first appears. Users might open existing records where your conditions are already met, and they need to see the mandatory indicators immediately. Copy your evaluation logic into the onLoad script so the form state is correct from the start.
Add server-side validation
Navigate to System Policy > Data Policies and create a policy that enforces the same mandatory logic server-side. Client scripts only work in the UI — web services, imports, and direct database updates bypass them completely. Set your condition logic in the data policy's Advanced tab using the same field evaluations as your client script.
Data policies use different syntax than client scripts — use current.field_name instead of g_form.getValue() in server-side conditions.
Best practices
Always pair client scripts with data policies for mandatory fields — client-side validation can be bypassed by web services and integrations.
Set both the true and false cases in your setMandatory logic — failing to clear mandatory when conditions change confuses users.
Use onChange scripts on the triggering field, not the field you're making mandatory — monitoring the wrong field means your logic won't fire when needed.
Test your logic with existing records, not just new ones — users open saved records where your conditions might already be met and need to see correct mandatory indicators immediately.
Avoid complex database queries in client scripts — they run in the browser and slow down the user experience, use GlideAjax if you need server data.
Test Your Knowledge
Quick 3-question quiz — see how your ServiceNow skills stack up.
A list view on a table with millions of records is slow. Best fix?
Select an answer to continue