Field validation needed to not have same users in 2 different fields

Ksnow
Tera Contributor

Hello all,

 

I have a requirement of field validation to not have selected same users in 2 fields.

There are 2 fields on the form, "Owner" and "Backup owner" both fields are reference to user table.

For example: In the Owner = John is selected, then Back owner field should not allow users to select John and Vice versa. (Same users cannot be selected in 2 fields)

How to achieve it?

Please help!

Appreciate your support!

 

Thanks,

Ksnow

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you need to use advanced ref qualifier in both the fields

Ensure you use correct field names below

Field 1 -> Owner

javascript:'sys_id!=' + current.u_backup_owner;

Field 2 -> backup owner

javascript:'sys_id!=' + current.u_owner;

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

View solution in original post

Pavankumar_1
Mega Patron

hi @Ksnow ,

For this you need to create 2 Onchange client scripts on reference fields.

 

1. create onchange client script on Back owner and use below script and replace with your field names

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

    var owner = g_form.getValue('u_owner'); // give owner refernece field to get value
    if (owner == newValue) {
        g_form.clearValue('u_back_owner'); //give your back owner fied backend name
		g_form.showFieldMsg('u_back_owner','Owner and back Owner should not be the same','error' ); //you can show field message if both reference are same
    }

}

 

2. Create onchange client script and select Owner reference field. use below script and replace with your field names

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

    var backowner = g_form.getValue('u_back_owner'); // give back owner refernece field to get value
    if (backowner == newValue) {
        g_form.clearValue('u_owner'); //give your owner fied backend name
		g_form.showFieldMsg('u_owner','Owner and back Owner should not be the same','error' ); //you can show field message if both reference are same
    }

}

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you need to use advanced ref qualifier in both the fields

Ensure you use correct field names below

Field 1 -> Owner

javascript:'sys_id!=' + current.u_backup_owner;

Field 2 -> backup owner

javascript:'sys_id!=' + current.u_owner;

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

Thanks for your response Ankur.

 

It's working fine on the form level, how to make it work on the record producer?

Is it different process? because I tried same in Record producer but it's not working.

 Appreciate your help!

 

Thanks,

Ksnow

@Ksnow 

You mentioned that my logic is working fine.

But you marked other response as correct.

Your question was for fields which I answered.

I believe I answered your original question.

Would you mind marking my response as correct?

For your subsequent question on how to use for record producer here is the approach

Ensure you use correct variable names below

Field 1 -> Owner variable

javascript:'sys_id!=' + current.variables.u_backup_owner;

Field 2 -> backup owner variable

javascript:'sys_id!=' + current.variables.u_owner;

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

Pavankumar_1
Mega Patron

hi @Ksnow ,

For this you need to create 2 Onchange client scripts on reference fields.

 

1. create onchange client script on Back owner and use below script and replace with your field names

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

    var owner = g_form.getValue('u_owner'); // give owner refernece field to get value
    if (owner == newValue) {
        g_form.clearValue('u_back_owner'); //give your back owner fied backend name
		g_form.showFieldMsg('u_back_owner','Owner and back Owner should not be the same','error' ); //you can show field message if both reference are same
    }

}

 

2. Create onchange client script and select Owner reference field. use below script and replace with your field names

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

    var backowner = g_form.getValue('u_back_owner'); // give back owner refernece field to get value
    if (backowner == newValue) {
        g_form.clearValue('u_owner'); //give your owner fied backend name
		g_form.showFieldMsg('u_owner','Owner and back Owner should not be the same','error' ); //you can show field message if both reference are same
    }

}

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar