No code Clear the Variable value / Set Variable values (Washington DC)
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:
Since Washington DC:
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:
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.
---
That's it. Hope you like it. If any questions or remarks, let me know!
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
9x ServiceNow MVP
---
https://www.servicenow.com/community/developer-articles/no-code-clear-the-variable-value-set-variable-values-washington/ta-p/2964567
Mark Roethof