- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 12:46 AM
Hi! I'm trying to compare 2 fields (FieldA, FieldB) in a form. Both fields' type is Choice. FieldB must be different from FieldA. An error message will pop-up when the selected value for FieldB is the same as FieldA. I've created an onchange client script but it seems to be not working:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 01:13 AM
you will have to write 2 onchange client scripts for this
1st on field A and another on field B
why not use single onSubmit and validate it?
Note: the below script will work only when both the field have exact same choice values, if not then it won't work
function onSubmit() {
var fieldA = g_form.getValue('FieldA');
var fieldB = g_form.getValue('FieldB');
if (fieldA && fieldB) {
if (fieldA === fieldB) {
alert('Your Message Here.');
return false;
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 12:58 AM
Hello @Momiji,
Here's a more reliable version of your onChange script, assuming that your onChange client script running on FieldB:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var fieldA = g_form.getValue('FieldA');
var fieldB = newValue; // use the newValue parameter directly for FieldB
if (fieldA && fieldB) {
if (fieldA === fieldB) {
alert('Your Message Here.');
}
}
}
Please mark my response as Accepted and Helpful for future references.
Best Regards,
Anshul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 01:03 AM - edited 08-05-2025 01:07 AM
Hi @Momiji ,
Please try the below:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.clearMessages();
var fieldA = g_form.getValue('u_choice_1');
if (fieldA && newValue) {
if (newValue === fieldA) {
g_form.addErrorMessage('Selection of FieldB must be different from FieldA');
}
}
}
Also, make sure the backend name of the choices for both the fields are same:
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 01:13 AM
you will have to write 2 onchange client scripts for this
1st on field A and another on field B
why not use single onSubmit and validate it?
Note: the below script will work only when both the field have exact same choice values, if not then it won't work
function onSubmit() {
var fieldA = g_form.getValue('FieldA');
var fieldB = g_form.getValue('FieldB');
if (fieldA && fieldB) {
if (fieldA === fieldB) {
alert('Your Message Here.');
return false;
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader