Which executes first ,UI policy action or UI policy script?

Rohith16
Tera Contributor

When runnning ui policies ,which executes first ,whether UI policy action or UI policy script?

26 REPLIES 26

kavinderr
ServiceNow Employee
ServiceNow Employee

Hi Rohith, best way to answer this would be to create a policy with script and return true/false from the script. If policy actions are still executed, you will understand that actions are based on the outcome of script. Thus order would be:
Policy Script >> Policy Actions.
But if this does not happen, you conclude the opposite.
The documentation say
UI Script
Client Script
UI policy.
But its not very clearly written whats happen in UI policy. We know for sure that condition dictate policy actions, and to my understanding policy scripts is just an extension of conditions

PunithaDinesh
Tera Contributor

UI policy Action first and then UI policy script.

 

Please refer: https://youtu.be/TptpAXlLT10?si=BmXX4_ybR4RYZdVA

TaironeA
Tera Expert

Execution Order in UI Policies

When running UI Policies in ServiceNow, the execution order is:

UI Policy Conditions – First, ServiceNow checks if the conditions of the UI Policy are met.
UI Policy Actions – If the conditions are met, UI Policy Actions (e.g., setting fields mandatory, visible, or read-only) execute first.
UI Policy Scripts – After UI Policy Actions execute, any associated UI Policy Scripts run.

Why Does UI Policy Action Execute First?

  • UI Policy Actions are declarative (they directly modify UI elements).
  • UI Policy Scripts allow for more complex logic, so they execute after actions to override or extend behavior if needed.

Example Case

Let's say you have a UI Policy:

  • Condition: If priority == 1
  • Action: Set description field to mandatory
  • Script: Log a message in the console

When triggered:
The description field becomes mandatory first (UI Policy Action).
Then, the UI Policy Script executes (e.g., logs to console).

Conclusion

UI Policy Actions execute first, followed by UI Policy Scripts.
This ensures that UI behavior changes happen before any custom logic runs.

Hope this helps! Let me know if you need further clarification.

(If you found this helpful, don’t forget to give a thumbs-up!)

Gopi22
Giga Guru

Hi, 

UI policy script executes first, and then the action. 

Here's how you can test it: 

Create a simple UI policy on any form, provide an alert statement in the script, 'Execute if true' part, save the UI policy. Then add a new action to make a field mandatory. 

When the UI policy conditions are met, you can see that first the alert statement is shown and after that the field is made mandatory. 

 

So that confirms, in a UI policy, script is executed first and then the action. 

 

Thanks,

Gopikrishnan

Robert H
Mega Sage

Hello @Gopi22 ,

 

Please note that an alert() is executed immediately and essentially freezes the browser and all other scripts. On the other hand, a lot of the ServiceNow UI logic is running asynchronously.

So what happened in your example is that the UI Policy Action was triggered first (starting to make the field mandatory asynchronously), then the UI Policy Script was invoked and essentially interrupted what the Action was doing.

 

So as numerous others have already said, the execution order is UI Policy Action first, then UI Policy Script.

You can verify this replacing your alert() with "g_form.setMandatory(yourFieldName, false);". The field will end up being non-mandatory, meaning the script ran later.

 

Regards,

Robert