UI policy or client script which has more preference??

ZebaT
Tera Contributor

Hi Everyone,

If UI policy makes field mandatory and I use client script to make it non mandatory which one will work?

On-change client script I understand will make it in non mandatory, what about client on load script?

 

Regards,

Zeba

6 ACCEPTED SOLUTIONS

J Siva
Kilo Patron

Hi @ZebaT 

In ServiceNow, when both a Client Script and a UI Policy apply to the same field, the execution order matters. Client Scripts run first, followed by UI Policies.
So, in your scenario:

  • If the Client Script makes the field non-mandatory,
  • The UI Policy executes afterward and overrides it, making the field mandatory again.

This means the UI Policy will take precedence because it runs later in the sequence.

 

Regards,
Siva

View solution in original post

Mohammed8
Giga Sage

Hi @ZebaT 

I agree with @J Siva here.

Client on load executes first, followed by UI policy. You can test it out in PDI:

 

1) Create a UI policy making Description field mandatory:

 

Mohammed8_0-1765517517036.png

 

2) Create a client script on load making Description non-mandatory: 

 

Mohammed8_1-1765517594192.png

 

3)Result: we can see, UI policy executes after Client script on-load:

 

Mohammed8_2-1765517674673.png

 

lets reverse condition and check again, UI policy makes Description field non-mandatory

 

Mohammed8_3-1765517804564.png

 

2) Client scripts for Description Field making it Mandatory:

 

Mohammed8_4-1765517850511.png

 

3) Result: Again UI policy executes after client script, hence Description is non-mandatory:

 

Mohammed8_5-1765517921871.png

 

 

If you find this answer useful, please mark it as helpful/solution accepted

 

Thanks and Regards,

Mohammed Zakir

View solution in original post

ZebaT
Tera Contributor

Thanks @J Siva  @Ankur Bawiskar @Mohammed8  for reply,

one more thing I wanted to know

For example if I want to make 5 field mandatory, should I go for client script or one UI policy with 5 action. What's best practice. What I have read is UI policy for simple is best practice. What's your opinion on this?

 

Thanks,

Zeba

View solution in original post

nayanmule
Tera Guru

In between UI Policies and Client Scripts , Client scripts execute first always and then UI policies run.

Hence, UI policies will always override the client script's logic.

for e.g  If you are making a field mandatory using Client script and thus adding a UI policy to make the same field non mandatory. The result will end up like the field is non mandatory as the logic of the UI policy executes after the client script.

 

Hence, Client script does preceed UI policy. But, the UI policies still win by overriding the logic .

 

If my response was helpful, please mark it and accept the solution.

View solution in original post

PrashantLearnIT
Tera Sage

HI @ZebaT 

 

Obviously, Client Script runs first, and UI Policy Runs after the client scripts is executed.

 

Talking about preferences of being the right one, that depends on what you are trying to achieve in terms of requirements or real use cases.

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

View solution in original post

@ZebaT 

It depends on use case. If its simple and straight forward go for UI policy and UI policy actions and its beginner friendly.

Here is the link having same discussion:

https://www.servicenow.com/community/developer-forum/ui-policy-and-client-script-best-practice/td-p/...

 

Thanks and Regards,

Mohammed Zakir

View solution in original post

6 REPLIES 6

J Siva
Kilo Patron

Hi @ZebaT 

In ServiceNow, when both a Client Script and a UI Policy apply to the same field, the execution order matters. Client Scripts run first, followed by UI Policies.
So, in your scenario:

  • If the Client Script makes the field non-mandatory,
  • The UI Policy executes afterward and overrides it, making the field mandatory again.

This means the UI Policy will take precedence because it runs later in the sequence.

 

Regards,
Siva

Mohammed8
Giga Sage

Hi @ZebaT 

I agree with @J Siva here.

Client on load executes first, followed by UI policy. You can test it out in PDI:

 

1) Create a UI policy making Description field mandatory:

 

Mohammed8_0-1765517517036.png

 

2) Create a client script on load making Description non-mandatory: 

 

Mohammed8_1-1765517594192.png

 

3)Result: we can see, UI policy executes after Client script on-load:

 

Mohammed8_2-1765517674673.png

 

lets reverse condition and check again, UI policy makes Description field non-mandatory

 

Mohammed8_3-1765517804564.png

 

2) Client scripts for Description Field making it Mandatory:

 

Mohammed8_4-1765517850511.png

 

3) Result: Again UI policy executes after client script, hence Description is non-mandatory:

 

Mohammed8_5-1765517921871.png

 

 

If you find this answer useful, please mark it as helpful/solution accepted

 

Thanks and Regards,

Mohammed Zakir

ZebaT
Tera Contributor

Thanks @J Siva  @Ankur Bawiskar @Mohammed8  for reply,

one more thing I wanted to know

For example if I want to make 5 field mandatory, should I go for client script or one UI policy with 5 action. What's best practice. What I have read is UI policy for simple is best practice. What's your opinion on this?

 

Thanks,

Zeba

@ZebaT 

It depends on use case. If its simple and straight forward go for UI policy and UI policy actions and its beginner friendly.

Here is the link having same discussion:

https://www.servicenow.com/community/developer-forum/ui-policy-and-client-script-best-practice/td-p/...

 

Thanks and Regards,

Mohammed Zakir