The Zurich release has arrived! Interested in new features and functionalities? Click here for more

priority of client script and ui policy

aditya2byte
Tera Contributor

ui policy        setvisible=true

client script   setVisible=false

Clien script over rides ui policy

 

but for  setmandatory and set readonly ui policy over rides client script 

so why in first case ,client script over rides ui policy

1 ACCEPTED SOLUTION

Ajay_Chavan
Kilo Sage
Why Client Script Overrides UI Policy for setVisible:

Timing: Client scripts run after UI policies
Last Action Wins: Whatever executes last determines final visibility
No Form Validation: Visibility is just DOM manipulation


Why UI Policy Overrides Client Script for setMandatory/setReadonly:

Form Engine Control: UI Policy engine has higher precedence for form validation
Built-in Protection: ServiceNow protects mandatory/readonly settings at form level
Validation Priority: Form validation rules override client-side changes

Execution Order:
1. Form Loads
2. UI Policies Execute  
3. Client Scripts Execute (onLoad)
4. Field Changes → UI Policies → Client Scripts (onChange)

Summary:
setVisible: Pure DOM manipulation → Last script wins (Client Script)
setMandatory/setReadonly: Form validation → UI Policy precedence (System protection)
Key Point: ServiceNow treats visibility as presentation but mandatory/readonly as form validation rules with different priority systems.

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

View solution in original post

9 REPLIES 9

Bhimashankar H
Mega Sage

Hi @aditya2byte ,

 

Are you using all three on same field? check script should not run after all loading then it will override.

 

Starting with the Istanbul release, ServiceNow began intentionally blocking g_form.setVisible() on mandatory fields. This ensures that the user can always see and fill out a required field, regardless of any conflicting client script logic. Before Istanbul, a client script with setVisible=false could successfully hide a field, potentially conflicting with UI policies and causing issues.

 

As always Client Script first then UI Policy runs.

 

Refer this article from SN: KB Article on setMandatory, setVisible issue 

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

im not implementing all  3 conditions at time,im executing these 3 conditions  separately .

For  setReadonly and setMandatory ui policy over ride the client script but for setVisible ui policy doesn't

override the client script , so why

?

@aditya2byte  Sometimes there be race condition between UI Policy and Client Script and we run into unexpected behaviour.

 

So follow the best practice on both the thing, do no use both on same variable. 

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Bhuvan
Kilo Patron

@aditya2byte 

 

Best practice is not to use both client script and UI policy on same field. Where possible, create UI policy as it is the recommended option.

 

The issue you are facing could be due to race condition between Client Script and UI Policy and sometimes behavior could be unexpected due to exception scenarios. Refer below link for more details,

 

https://www.servicenow.com/community/developer-forum/client-script-overriding-the-ui-policy-and-maki...

 

Mitigate this by having only UI policy and removing client script as both achieve same function.

 

If this helped to answer your query, please mark it as helpful & accept the solution. 

 

Thanks,

Bhuvan