Comparing 2 fields while filling up a form

Momiji
Tera Contributor

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:

 

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

    var fieldA = g_form.getValue('FieldA');
    var fieldB = g_form.getValue('FieldB');

    if (fieldA && fieldB) {
        // Check if both fields have values
        if (fieldA === fieldB) {
            // If both fields have the same value, show an alert          
            alert('Selection of FieldB must be different from FieldA');
        }
    }
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Momiji 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

anshul_goyal
Kilo Sage

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

 

SN_Learn
Kilo Patron
Kilo Patron

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');
        }

    }
}

 

image.png

image.png

Also, make sure the backend name of the choices for both the fields are same:

image.pngimage.png

 

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Ankur Bawiskar
Tera Patron
Tera Patron

@Momiji 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader