- 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:49 PM
I have shared the workaround below
Thank you for marking my response as helpful.
If my response helped you please mark it correct to close the question so that it benefits future readers as well.
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:42 PM
So to make your logic work do this
1) create hidden variable of type String and hide it always using UI policy on form load
2) whenever user selects Yes set the hidden variable with Yes
3) when user changes value from Yes clear it
4) now use this hidden variable value in your script for comparision
Script like this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading){
return;
}
if(newValue == 'Yes'){
g_form.setValue('hiddenVariable', 'Yes');
}
else if(newValue == 'No' || 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-15-2021 12:28 AM
When value changes from Yes to No, I need to do my logic.
So with this script, when value is Yes we are setting "hidden=yes", if value changes from (yes to no) hidden value will set to empty , because we are clearing the value (newValue = no)
Then in the if loop g_form.getValue('hidden') wil always empty only right? again our validation will fail.
- 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