Clear the field value / Clear the variable value - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

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

 

There is a newer version available for this content: "No code Clear the Variable value / Set Variable values (Washington DC)

 

Hi there,

 

A short article to share gained knowledge about Clearing the field / variable value. Not entirely new functionality anymore, as this is already available from London! Though I didn't find any articles, blogs, videos on this. So writing everything out to share with the Community.

The "issue"

When working with conditional visible fields / variables, these will mostly get visible thru (Catalog) Client Scripts or (Catalog) UI Policies. If you would fill a conditional visible field / variable, and then change the value of the field / variable will trigger (Catalog) Client Script or (Catalog) UI Policy which made the conditional field visible, the conditional visible field / variable might not be visible anymore. Though, what happens with the value you entered? If you do nothing... the value will just stay there. So actually if you would save the form the value will be stored, even if the field / variable is not visible.

 

This behavior doesn't have to be an issue, but in some cases could well be an issue! For example if other depending scripting is involved, what about reporting, list views, etc..

 

A picture says more than a thousand words... so an out-of-the-box example:
- Platform UI
- Story [rm_story]
- Select an active Story
- Check "Blocked"
- Enter anything you'd like in the "Blocked reason" field
- Uncheck "Blocked", "Blocked reason" becomes hidden again
- Save the Story

 

Notice that your "Blocked reason" is not saved, as the field isn't visible. This is actually due to Client Script "Clear Blocked Reason". If you would deactivate this Client Script, on save of the Story, the "Blocked reason" would remain even if the field isn't visible.

Pre-London

In the old days, you could add some limited scripting to empty the field / variable value. Thru both (Catalog) Client Script and (Catalog UI Policy). Notice the Client Script mentioned above, a great example of how this was done in the old days!


The scripting for this Client Script is only:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue == '') {
		return;
	}

	if (newValue+"" == "false"){
		g_form.setValue("blocked_reason", "");
	}
}

 

For (Catalog) UI Policies something like below could do the job:

(Catalog) UI Policy, "Execute if false"

 

function onCondition() {
	g_form.setValue('field_name', '');
}

 

London

With the introduction of London, the (Catalog) UI Policy Actions got enriched with a checkbox "Clear the field value" / "Clear the variable value". It does exactly as the label describes... if the (Catalog) UI Policy condition is true, and "Clear the variable value" / "Clear the field value" is selected, then it will be cleared.

 

find_real_file.png

 

Important note:
One thing to point out. You might have been used to set up (Catalog) UI Policies with conditions which would end up being true, and then adding (Catalog) UI Policy Actions which would, for example, make Visible is true, etc.. Though if you would 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.

 

The release notes describe this functionality pretty short. Anyway, a link to the release notes:
https://docs.servicenow.com/bundle/london-release-notes/page/release-notes/it-service-management/ser...

---

And that's it actually. Not much to it.

 

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

ServiceNow Technical Consultant @ Paphos Group
---

LinkedIn

Comments
Niclas
Giga Guru

Sounds like a good approach to design UI Policy Actions to set the visibility of variables to false.

Seems like the "revert if false" setting on the UI policy is irrelevant for the "clear variable" checkbox. This means setting a variable to visibile = true and clear value = false would only hide, but not clear the variable, if the UI policy condition evaluates to false and "revert if false" is activated. As a result a previous entered/selected value would still pop up in the my requested item options summary, although being irrelevant - and this might confuse users. 

robertyoung
Giga Contributor

I would go so far as to say that this functionality just doesn't work at all under London. Under what conditions DOES it actually clear the values? When it goes back to true? That isn't helpful.

Mark Roethof
Tera Patron
Tera Patron

Could you elaborate? What is not working in your opinion?
It works fine for us, and several community threats were helped with this. Works in London / Madrid / New York.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

Mary Beth Hutch
Tera Guru

THANK YOU for this... we were scratching our heads trying to figure out why this was the problem!

Martijn Olthuis
Tera Contributor

Thank you Mark. This is very useful article.

 

find_real_file.png

Sivakrishna
Tera Guru

Dear Team,

Actually I have a Record producer Catalog Item in Employee Service Portal i.e. Personal Data changes. When ever a user selected this personal data changes form. This form will give you the 2 check boxes as below:

1. Name change

2. Bank information Change

Whenever the user selects 1st check box i.e. "Name change". Two(2) variables will be displayed as below in Employee service portal:

a. Enter effective date of legal name change: 

b: Enter new legal name:

 

When ever user submitted the form with required values for the above two variables(a,b) , a HR case record will be opened in HR case table. When I opened the HR case in native UI portal, the above two variables(a,b) not visible in the HR case form due to I have made the "Clear variable value" check box as "false" for the both the variables(a,b) in the Record Producer Catalog UI actions. 

 

When ever I made the "Clear variable value" check box as "true" for the both the variables(a,b) in the Record Producer Catalog UI actions then the variables are visible in the HR case form without user input values i.e. the variables are blank without user input.

 

Please fix this issue immediately. I am scratching our heads trying to figure out why this was the problem!

 

Thanks

 

P. Sivakrishna

Mark Roethof
Tera Patron
Tera Patron

"Please fix this issue immediately"

 

Who are you mentioning this to? We are all random Community members who are posting here voluntarily. If you want immediate help, hire someone.

Sivakrishna
Tera Guru

@Mark Roethof 

 

Sorry for that one "Mark Roethof". I have posted that issue to get the solution in a priority. Once again sorry.

 

Thanks

 

P.Sivakrishna

 

HugoFirst
Kilo Sage

Hello from ther future ( 2024 ) and I applaud this article.  We have a complicated form which was passing values to the RITM which were not displayed on the form when it was submitted.  This article helped us undertand why and make the changes necesary to fix it.

Version history
Last update:
‎08-21-2024 09:03 PM
Updated by:
Contributors