- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2022 10:22 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2022 10:45 PM - edited 10-09-2022 10:46 PM
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;
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2022 11:39 PM
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
}
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2022 10:45 PM - edited 10-09-2022 10:46 PM
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;
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2022 11:20 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 12:50 AM
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;
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2022 11:39 PM
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
}
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar