We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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?

27 REPLIES 27

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

 

Hi @Robert H , 

 

I tested as you suggested and I confirm what you mentioned is right. 

 

Learned something new today, Thank you ! 🙂 

To @anyone who is looking for a confirmed answer here - UI Policy actions gets executed first and then the script. 

 

Thanks,

Gopikrishnan

NamanChaturvedi
Tera Contributor

Hi @Rohith16

So try a scenario, create an UI policy on incident table, create an UI action to make description field mandatory, under when to apply -> priority is 1-critical. Also create a script in -> Execute if true section - alert("Description mandatory now for P1 incident"). 

Create an incident with this now, you'll notice that the alert runs first, then the description becomes mandatory, which suggests that - UI policy script runs first then the UI policy action runs.

Mark my answer as helpful if applicable.

Thanks
Naman Chaturvedi