You are on page 1of 12

!

Blog

A Race between UI Policy and Client script


by Deepak Ingale Forum Level 10
created 6y ago (edited 6y ago) in Developer Community

A Race between UI Policy and Client


script
I happened to work on one issue in past for debugging UI Policies and client
scripts.
Issue was something like:
For one particular state, UI Policy was suppose to set some Ields as Mandatory
and it was not happening. This was happening when form was loading.
I checked other UI policies as well which were of   higher order than the one which
was not working as expected.   But I   could not Ind anything.
I was under the perception that client scripts always run Irst and then the UI
Policies. But, I Igured out one exception to this case which is being documented
in this blog.
So let us start off with some test cases which I had built for this experiment. I
have used multiple client scripts and UI Policies for this testing and used one
global scratchpad variable to keep track of when particular script gets Ired.
Steps :
1) ConIgure onLoad client script , you can use any table, In my experiment, I have
used "Database Instance"
Expected result : set "Description (short_description) " Ield as "Mandatory" when
script is executed.
I got my expected result when this script got executed, so far so good.

2) Now let us try to prove the fact that UI Policies are executed after the client
script.
So now let's conIgure the UI Policy as show below.
I am now setting "Description" Ield to "Non Mandatory" to test if result of client
script are applied in case 1 or result of this UI Policy is applied when form is
loaded
Once done, open any of the record form on "Database Instance".
You will notice two alert messages, Irst alert message comes from onLoad client
script which we built in step 1 and second message comes from UI Policy which
we built in   this step.
Now let's try to Igure out what is happening
a) Our onLoad client script when gets Ired ahead of UI Policy, it tries to set
"Description" Ield as mandatory.

b) UI Policy when gets Ired after onLoad client script, it then tries to set
"Description" Ield as non mandatory.

Now let's see what we get on the form.


We see "Description" Ield is non mandatory. And this is the expected behavior as
well since UI Policies are executed later than client script.
3) Now conIgure onChange client script which gets Ired when Ield "Operational
Status" changes
:
Now intension is to set "Description" Ield as Mandatory.
Note that, we expect this script should gets Ired only when Ield value on
"Operational Status" changes.
Let's see what we get
.

4) Now, let's conIgure new UI Policy, lets make this UI Policy as conditional.
ConIgure the condition when operation status is   "Non - Operational", this script
should Ire and set "Description" Ield is "Non Mandatory"
:
Now change the "Operational " to "Non Operational" and see what we get. You will
notice Irst our "onChange" client script gets Ired, and then our "Conditional UI
Policy" which we have conIgured in this step
:
This is also an expected result since UI Policy is Ired at the very last, so far so
good.
5) ConIgure onChange client script as per below screenshot. You will see there is
an "isLoading" property which we have used in this script.

Reload the form and see observations.


:
You will notice
1) onLoad client script is executed Irst when form is loading
2) UI Policy which has no condition is executed, it has a lower order compared to
next UI Policy
3) UI Policy which is conditional is executed
4) 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 Ield to alter its behavior since it is the part
which got Ired at very last. Someone has set the Ield action of mandatory is this
portion of the script which was con]icting with the UI Policy.
Some of the observations:
1) onLoad client scripts are Ired Irst
2) UI Policy with increasing order are Ired.
:
3)onChange client scripts gets executed (onChange) if you put something into the
isLoading portion when form is loaded irrespective of Ield is changed or not. Also
it gets executed after the UI Policy.
So make sure not to set @elds 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.
4) onChange client script gets executed if Ield value is changed for which it is
conIgured, also make sure to put return is true in isLoading portion.

Kindly do not forget to like or bookmark this post if it


assists you.
Topics: Scripting and Coding

" Helpful (103) # Comment (13) 22864 Views

13 Comments Show All Comments

BenPhillipsSNC Forum Level 3 • 5y ago

Good stuff! Thank you.

" Helpful (0) # Comment

sudarshanpuppala • 5y ago
S

Really good research and perfect explanation. Thank you for your great effort here.

" Helpful (1) # Comment


:
mallikharjunaswamyvutla • 4y ago

could you please explain why client scripts get executed Irst?

" Helpful (0) # Comment

$ Show comments

kshitij • 4y ago
K

really good work tq

" Helpful (0) # Comment

manukundur • 4y ago
M

Very nice and detailed explaination. It really helps thanks for knowledge share.

" Helpful (0) # Comment

Venu Suri • 4y ago


VS

very nice and good info

" Helpful (0) # Comment

Santosh Kshirsagar Forum Level 3 • 4y ago

In my case UI Policy executed Irst: 


Step1:-
:
Written an onLoad client script to make a Ield invisible.
Step2 : -
Written UI policy without condition to make the same Ield visible.  
Result: The Ield was Invisible.
 
Could anyone explain how it was executed?

" Helpful (0) # Comment

$ Show comments

Pranshu • 3y ago

Well explained, thank you!

" Helpful (0) # Comment

robhumphries Forum Level 1 • 3y ago

I have also found that if an onLoad client script uses GlideAjax the onLoad UI Policies will run
while the asynchronous Ajax response is coming back.
 

" Helpful (3) # Comment

Arpitha Gopalswamy • 3y ago


AG

Well explained..

" Helpful (0) # Comment


:
Alexander Vershinskiy • 2mo ago

Were there any recent changes in San Diego to the order execution of any of these objects? we
can see different behavior in San Diego Instance vs Rome instance.
we have both UI Policy and client Script, but seems like after San Diego, their execution order
changed....
we are keep looking, but never hurts to double check.

" Helpful (0) # Comment

 
:
 

Contact Us % & ' (

Help
Terms of Use
Privacy Policy
Cookie Preferences
Trademark and Branding
ServiceNow Support
Knowledge Base

© 2022 ServiceNow. All rights reserved.


:

You might also like