The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Deepak Ingale1
Mega Sage

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 fields 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 find anything.

I was under the perception that client scripts always run first and then the UI Policies. But, I figured 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 fired.

Steps :

1) Configure onLoad client script , you can use any table, In my experiment, I have used "Database Instance"

find_real_file.png

Expected result : set "Description (short_description) " field as "Mandatory" when script is executed.

I got my expected result when this script got executed, so far so good.

find_real_file.png

find_real_file.png

2) Now let us try to prove the fact that UI Policies are executed after the client script.

So now let's configure the UI Policy as show below.

I am now setting "Description" field 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

find_real_file.png

find_real_file.png

Once done, open any of the record form on "Database Instance".

You will notice two alert messages, first 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 figure out what is happening

a) Our onLoad client script when gets fired ahead of UI Policy, it tries to set "Description" field as mandatory.

find_real_file.png

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

find_real_file.png

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

We see "Description" field is non mandatory. And this is the expected behavior as well since UI Policies are executed later than client script.

3) Now configure onChange client script which gets fired when field "Operational Status" changes

find_real_file.png

Now intension is to set "Description" field as Mandatory.

Note that, we expect this script should gets fired only when field value on "Operational Status" changes.

Let's see what we get

.

find_real_file.png

find_real_file.png

4) Now, let's configure new UI Policy, lets make this UI Policy as conditional.

Configure the condition when operation status is   "Non - Operational", this script should fire and set "Description" field is "Non Mandatory"

find_real_file.png

find_real_file.png

Now change the "Operational " to "Non Operational" and see what we get. You will notice first our "onChange" client script gets fired, and then our "Conditional UI Policy" which we have configured in this step

find_real_file.png

find_real_file.png

find_real_file.png

This is also an expected result since UI Policy is fired at the very last, so far so good.

5) Configure onChange client script as per below screenshot. You will see there is an "isLoading" property which we have used in this script.

find_real_file.png

Reload the form and see observations.

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

You will notice

1) onLoad client script is executed first 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 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.

Some of the observations:

1) onLoad client scripts are fired first

2) UI Policy with increasing order are fired.

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

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.

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

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

15 Comments