- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2019 10:59 PM
Which runs first UI policy or Client script?Best Practices? Differences?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2019 11:09 PM
Hello Biblo,
Check out my finding about UI policy, Client scripts:
Which runs first UI policy or Client script?
1. UI Policies execute after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies.
2. You will notice
- onLoad client script is executed first when form is loading
- UI Policy which has no condition is executed, it has a lower order compared to next UI Policy
- UI Policy which is conditional is executed
- onChange client script gets executed, but if you closely notice, it the "isLoading" part within a client script is getting executed. This is really important and stood out to be the culprit which was causing field to alter its behavior since it is the part which got fired at very last. Someone has set the field action of mandatory is this portion of the script which was conflicting with the UI Policy
- onLoad client scripts are fired first
- UI Policy with increasing order are fired.
- onChange client scripts gets executed (onChange) if you put something into the isLoading portion when form is loaded irrespective of field is changed or not. Also it gets executed after the UI Policy.
- So make sure not to set fields as mandatory or read only or visible using isLoading property. If you encounter similar issue, that would be hard to debug. Make sure to use "UI Policies" for this case always.
- onChange client script gets executed if field value is changed for which it is configured, also make sure to put return is true in isLoading portion.
Best Practices?
1.Set Client Script Order
2.Use Client Scripts to Validate Data
3. Use UI Policies Instead of Client Scripts to Set Field Attributes
4. Use setValue()’s displayValue Parameter with Reference Fields
5. Reduce server lookups
6. Avoid global client scripts
7. Add the isLoading check to onChange scripts
8. Add the newValue check
Differences?
Criteria |
Client Script |
UI Policy |
Execute on form load |
Yes |
Yes |
Execute on form save/submit/update |
Yes |
No |
Execute on form field value change |
Yes |
Yes |
Have access to field's old value |
Yes |
No |
On Change on Multiple Field |
No |
Yes |
Execute after Client Scripts |
No |
Yes |
Set field attributes with no scripting |
No |
Yes |
Require control over order of execution |
*Yes |
Yes |
I hope these points will be enough. Other than this, you can check out developer documents suggested by Manish.
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2019 11:03 PM
This should answer your query:
https://developer.servicenow.com/app.do#!/training/article/app_store_learnv2_scripting_madrid_client...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2019 11:03 PM
Hi Bilbo,
Here is the precise order
1) onload client script
2) onLoad UI policy
3) onchange client script
4) onchage UI policy
best practices:
1) use client scripts where you have complex conditions/scripts which uses combination of different fields to handle mandatory, visibility, readonly
2) use UI policies for simple things like if state is closed then make fields as mandatory, visible etc
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2019 11:09 PM
Hello Biblo,
Check out my finding about UI policy, Client scripts:
Which runs first UI policy or Client script?
1. UI Policies execute after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies.
2. You will notice
- onLoad client script is executed first when form is loading
- UI Policy which has no condition is executed, it has a lower order compared to next UI Policy
- UI Policy which is conditional is executed
- onChange client script gets executed, but if you closely notice, it the "isLoading" part within a client script is getting executed. This is really important and stood out to be the culprit which was causing field to alter its behavior since it is the part which got fired at very last. Someone has set the field action of mandatory is this portion of the script which was conflicting with the UI Policy
- onLoad client scripts are fired first
- UI Policy with increasing order are fired.
- onChange client scripts gets executed (onChange) if you put something into the isLoading portion when form is loaded irrespective of field is changed or not. Also it gets executed after the UI Policy.
- So make sure not to set fields as mandatory or read only or visible using isLoading property. If you encounter similar issue, that would be hard to debug. Make sure to use "UI Policies" for this case always.
- onChange client script gets executed if field value is changed for which it is configured, also make sure to put return is true in isLoading portion.
Best Practices?
1.Set Client Script Order
2.Use Client Scripts to Validate Data
3. Use UI Policies Instead of Client Scripts to Set Field Attributes
4. Use setValue()’s displayValue Parameter with Reference Fields
5. Reduce server lookups
6. Avoid global client scripts
7. Add the isLoading check to onChange scripts
8. Add the newValue check
Differences?
Criteria |
Client Script |
UI Policy |
Execute on form load |
Yes |
Yes |
Execute on form save/submit/update |
Yes |
No |
Execute on form field value change |
Yes |
Yes |
Have access to field's old value |
Yes |
No |
On Change on Multiple Field |
No |
Yes |
Execute after Client Scripts |
No |
Yes |
Set field attributes with no scripting |
No |
Yes |
Require control over order of execution |
*Yes |
Yes |
I hope these points will be enough. Other than this, you can check out developer documents suggested by Manish.
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2020 12:22 AM
Check out this video, it will clear all your doubts and help you to understand Client Script queries in details.
Link: https://www.youtube.com/watch?v=Wj8ZAWtTBqo&ab_channel=ServiceNowHelpdesk
It help you to understand below points.
- UI policy and Client script
- why client script run first in service now?.
- Which rule execute first client Scripts or UI polices?
- g_user and g_form tips
- Difference between client script and script include (server).
- Scenario based implementation
- Understanding below
- onLoad()
- onChange()
- onSubmit()
- onCellEdit()
Please mark reply as Helpful/Correct, if applicable. Thanks!!