Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

When working with conditional visible variables, these will mostly become visible through Catalog Client Scripts or Catalog UI Policies. Did you realize, that when making variables conditionally hidden, possible entered values are not being cleared? That you need to consider clearing such values yourself, using a Catalog Client Script or the Clear the Variable value function ServiceNow introduced with the London family release which I wrote this article on almost five years ago:

- 2019-07-16 - Article - Clear the field value / Clear the variable value

 

Time for an update where it concerns the Clear the Variable value option. This function changed and enhanced with the Washington DC family release.

 

The issue

As mentioned in the introduction, when working with conditional visible variables, you might not realize that possible entered values are not cleared when conditionally hiding Variables through Catalog Client Scripts or Catalog UI Policies. This behavior doesn't have to be an issue, though in some cases could well be or become an issue! For example if other depending scripting is involved, what about reporting, list views, database footprint, etcetera.

 

Pre-London

In the old days, you could add some limited scripting to clear the variable value. Through both Catalog Client Script and Catalog UI Policy.

 

The scripting for Catalog Client Scripts and Catalog UI Policies would need something like:

g_form.setValue('field_name', '');

 

London

With the introduction of the London family release, the Catalog UI Policy Actions got enriched with a checkbox "Clear the Variable value" (this is also present on UI Policies to be used for the Core UI). It does exactly as the label describes... if the Catalog UI Policy condition is true, and "Clear the Variable value" is selected, then it will be cleared. No scripting is needed anymore!

 

One thing to point out. You might have been used to setting up Catalog UI Policies with conditions that would end up being true, and then adding Catalog UI Policy Actions which would, for example, make Visible is true, etcetera. However if you select Clear the field value, this would conflict... So actually you have to rethink the logic. The condition should end up being false, Visible could then be false and Clear the field value can be selected.

 

Washington DC

Almost five years later, ServiceNow changed and enhanced the Clear the Variable function. The changes were not clearly mentioned in the release notes, only the enhanced part is:

 

"Set values in Catalog UI Policy Actions for questions in a catalog item without the need to script. The value that you set for the question is displayed when the specified condition is met. You can set the values using Catalog UI Policy Actions in the Now Platform."

 

To visualize the changes, since London:

 

01.png

 

Since Washington DC:

 

02.png

 

The Clear the Variable value Checkbox type field has been replaced by a Choice type field with three options:

- "Leave alone": Doesn't need any explanation I assume, similar behavior as all the other functions on UI Policy Actions as it always been.

- "Set value": The enhanced part of the changes with the Washington DC release. It's now possible to set a value on variables without scripting! When selected, field "Value" becomes visible in which you can literally type the value you want to set for the variable. 

- "Clear value": does exactly the same as the previously used Checkbox to clear the variable value.

 

New field not visible

If the Instance you are working on, in the past the Catalog UI Policy Action Form Layout got changed, then these new fields will not automatically be added while upgrading to the Washington DC family release. A Skipped record would have been created for you to handle. Either handle the Skipped record or update the Form Layout manually.

 

The Catalog UI Policy Form Layout out-of-the-box looks like: 

 

03.png

 

At the same time as the new fields would not be visible, you would also experience the Clear the variable value checkbox not being visible anymore after upgrading to the Washington DC family release. This is due to the newly added UI Policy "Hide Clear Value".

 

The old Clear the Variable value field will still work. While testing, I noticed that you can use both. Good to know that if you selected a choice option for the new Value action field, this choice always wins and would be applied instead of the old Clear the Variable value.

 

Data migration

One of the first things that crossed my mind when seeing the enhancements, what happens to existing data? Should we manually update this? As mentioned the old Clear the Variable value will still work. So from that point of view it's not necessary to update existing Catalog UI Policy Actions. From a manageability perspective, it would be a good thing to update existing Catalog UI Policy Actions. Luckily ServiceNow already thought of this, while upgrading your instance to the Washington DC release a Script Action will run automatically to migrate the existing Catalog UI Policy Actions.

 

04.png

---

 

That's it. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

Independent ServiceNow Consultant
9x ServiceNow MVP

---

LinkedIn

Comments
jammer
Tera Expert

@Mark Roethof 

 

I'm seeing some funny results regarding this new 'Value action' field after upgrading to Washington.

We had many ui policy actions where 'Clear the variable value' was false going into the upgrade to Washington.  See first image below.  

 

However, after the upgrade, when I open one of those ui policy actions, the new field named Value action has been set to 'Clear value'.  See second image below.

 

Obviously, these 2 values are in contradiction to one another.  I believe you're correct when you say that it's the value for the Value action field that wins, but what if we didn't want Value action to get defaulted to 'Clear value'?  i.e. we don't want that field/variable value to be cleared if the ui policy condition is true.

Looking at the fix script named Migrate clear value actions, I don't believe that is what caused our issue as I think that script only updated 1 ui policy action.  However, after the upgrade, we have 422 ui policy actions where Clear the variable value is false AND Variable action is 'Clear value'.  Any idea on how this could have happened?  

jammer_0-1721748711650.pngjammer_1-1721748750412.png

 

jcwarren
Kilo Guru

The 'Clear Variable' option only seems to work when the form's conditions change via UI Policy, i.e., conditions are no longer true. We've found that if a given variable is visible and data is entered into the field but then the conditions are changed so that it is no longer visible, the field with data will be visible in downstream records, e.g., catalog tasks, even though it is no longer relevant. We get around that by using the clear variable code on the UI Policies. 

Rob Banks
Tera Contributor

@Mark Roethof 

Trying to figure out something related to this. Right now we have some forms we're building that have variables that are conditional but will also other variables that are conditional based on selection to that variable:

 

Variable 1 - "What are you requesting?" (Select Box with A and B choice)

Variable 2 - "Type of change needed?" (Select Box with A-D choices that only displays if Variable 1 is set to B)

Variables 3-7 are questions that specifically show up if Variable 2 is set to A

 

It seems that using the "Clear Value" choice on Variable 2 is only triggering specifically when Variable 1 is set to B. This makes sense ultimately because it states that the value would be cleared specifically when the condition is met. This doesn't solve the issue of variables 3-7 still displaying if a user goes back and changes variable 1 from choice B to choice A or none; variables 3-7 will disappear if you change variable 1 back to choice B. Is there something obvious I'm missing here?

Mary9
Tera Guru

@Rob Banks So does Variable 2 clear when Variable 2 is visible? I would update the UI Policy so that the condition evaluates to true when the variable should be hidden.

 

Condition:

Variable 1 != B

 

UI Action on Variable 2:

Visible = False

Clear = True

 

This way Variable 2 clears as soon as it is hidden, which will hide variables 3-7. Then when Variable 1 = B, Variable 2 will display with an already cleared value.

Rob Banks
Tera Contributor

@Mary9 Ahh, I see! I created a second action to hide and clear variable 2 if its conditional wasn't true, and since that clears variable 2 then the following variables don't have their conditional met. Fantastic, thank you. I (and others in my org working on this) are still new to ServiceNow and were assuming that if the variable wasn't visible on the form any longer that it was automatically clearing the field, which is not the case. Just need to be a bit more specific with our conditionals.

jnt-cnt
Mega Sage

Hi @jjammer 

Did you end up getting a solution to this or explanation?

jammer
Tera Expert

@jnt-cnt   It's been awhile since I've given this any thought, but I think since it didn't seem to impact the behavior of our ui policy actions I just left it alone and moved on w/o any solution/explanation.

Version history
Last update:
‎07-27-2024 11:12 AM
Updated by:
Contributors