UI policy and Client script

Ashwi
Tera Contributor

Which runs first UI policy or Client script?Best Practices? Differences?

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

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

3. Some of the observations:

-  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.

  1. 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

Thank you,
Abhishek Gardade

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

AbhishekGardade
Giga Sage

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

3. Some of the observations:

-  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.

  1. 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

Thank you,
Abhishek Gardade

Runjay Patel
Giga Sage

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!!