- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2021 10:43 PM
Hi All,
Its a Yes/No catalog variable. I have written a onchange client script. The requirement is something like "if the particular field changes from Yes to No, do the logic".
So i wrote like below, if (oldValue is Yes & newValue is No) but its not working. Even the alert in the 5th line is empty for oldValue, newValue alert is coming.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2021 12:37 AM
Hi,
then clear it only when the value selected is None
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading){
return;
}
if(newValue == 'Yes'){
g_form.setValue('hiddenVariable', 'Yes');
}
else if(newValue == ''){
g_form.clearValue('hiddenVariable');
}
if(g_form.getValue('hiddenVariable') == 'Yes' && newValue == 'No'){
// your logic
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2021 11:14 PM
So if the form loads and the none option is by default then it would be empty as
oldValue will be None
None -> means empty
If you don't want None and by default it should be Yes then don't include None
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2021 11:24 PM
Ya Ankur, none will be empty only.
But consider this scenario,
a) Form loads default value is none, now i change the value to yes.
So my oldValue is empty and newValue is Yes.
b) Now i change the value from Yes to No. In this case it should be
oldValue is Yes and newValue will be No.
Correct? This is the expected behaviour right?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2021 11:28 PM
No this is not correct. newValue does not work this way. That's why I was asking for more details on your case. What you are describing will not work with using oldValue. oldValue is the original value, this is not the value from before an onchange, its the original value from the form load.
Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2021 11:39 PM
Hi,
please check these points
1) oldValue: value of the field when the form loaded and prior to the change.
So it means oldValue is always None
2) the oldValue is the original value of the field when the page first loads, so doing a comparison against the oldValue is always comparing with the original field value.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2021 11:44 PM
Thanks Ankur, for the screenshot. But do you have any other idea how we can implement this, only when value changes from Yes to No.