Compare List Collector field with reference field in catalog client script

Jens Koellner
Tera Contributor

Hey,

am fairly new to all this js scripting stuff, and tried to find similarly stated topics for quiet a while, not resulting in success yet.

 

I do have a Catalog Item which (amongst others) does have the following 2 form fields (variables) I want to compare:

1. primary_contact - a Reference field referring to the User [sys_user] table

2. additional_approver - a List Collector field referring to the same table User [sys_user]

 

For the field "primary_contact" a single user will be selected.

For the field "additional_approver" 1-3 users will be selected.

 

Now, the aim is, 

  • to compare each single name contained in "additional_approver" with the name in "primary_contact"
  • If they are the same,
    • A message shall appear outlining that this particular user name has been used for "primary_contact" and must not be used in "additional_approver"
    • this particular (duplicate) name shall be deleted from "additional_approver", valid user names (previously checked and found correct users) shall be kept
  • if they are all different,
    • No action necessary

--

This is what I prepared so far, but am stuck at present:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//check approvers are different to primary contact
var approverNameCollection=[];
var primcontact = g_form.getValue('primary_contact');
var approverNameList=g_form.getValue('additional_approver');
var approvers=approverNameList.split(',');

for (var i=0;i<approvers.length;i++) {
var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id', approvers[i]);
gr.query();
while (gr.next()) {
approverNameCollection.push(gr.name);

if (approvers[i] == primcontact) {
alert("Primary Contact and AdminIT-Approver have to be different persons.");
g_form.clearValue(approverNameList, '');
}
}
}
}

 

--

Weird is, that in the SP nothing happens at all, whereas by using "Try It" in the CatItem in the backend at least the alert pops up.

 

Any help or suggestion is pretty welcome.

 

Thanks, Jens

1 ACCEPTED SOLUTION

Hello Jens,

 

Thanks for the response.

 

Ok , so now we have secondary approver as well and we don't want user selected in either Primary or in Secondary to be displayed in additional approver.

 

If my understanding is correct then  we just need to modify the reference qualifier on additional approver.

 

javascript&colon;"sys_id!=" + current.variables.primary_conatct + "^sys_id!=" + current.variables.secondary_contact; 

 

Hope this will work for you..!!

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

10 REPLIES 10

Vrushali  Kolte
Mega Sage

Hi @Jens Koellner ,

 

Can you try the below code:

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

    // Get the primary contact and additional approver values
    var primaryContact = g_form.getValue('primary_contact');
    var additionalApprovers = g_form.getValue('additional_approver');

    // Split the additional approver values into an array
    var approvers = additionalApprovers.split(',');

    // Create an array to store duplicate names
    var duplicateNames = [];

    // Check if any approver name matches the primary contact
    for (var i = 0; i < approvers.length; i++) {
        var gr = new GlideRecord('sys_user');
        if (gr.get(approvers[i])) {
            if (gr.name == primaryContact) {
                duplicateNames.push(gr.name);
            }
        }
    }

    // If there are duplicate names, display an alert and clear the field
    if (duplicateNames.length > 0) {
        alert("Primary Contact and Additional Approvers must be different persons. The following names are duplicated: " + duplicateNames.join(', '));
        g_form.clearValue('additional_approver');
    }
}

If my answer solves your issue, please mark it as Helpful 👍 and Accepted ✔️ based on impact.

Thanks

Hey Vrushali,

upfront, many thanks for your reply. Appreciated.

 

Unfortunately the script doesn't get to the results I'd expect. 

It's still possible to select for the additional_approver the same name I selected for primary_contact. 

 

Regards,

Jens

Vishal Birajdar
Giga Sage

Hello Jens,

 

If my understanding is correct , you don't want User selected in primary contact  to be in  "additional_approver" list variable.

My suggestion will be to write reference qualifier on "Additional approver" variable so that User selected in "Primary contact" will not be available in "Additional approver".

 

Please find below solution :

 

VishalBirajdar7_0-1693805440835.png

 

Reference qualifier : javascript&colon;"sys_id!=" + current.variables.primary_conatct;  //you can use your variable name here for primary_conatct

 

Please find the output I'm getting

 

1.Test case  : If I select "Abel Tutor" in primary contact  then I'll not able to see "Abel Tutor" in Additional Approver

 

VishalBirajdar7_1-1693805655578.png

2. Test Case : If I select "Abraham Lincoln" in primary contact  then I'll not able to see "Abraham Lincoln" in Additional Approver

 

VishalBirajdar7_2-1693805797938.png

This approach will reduce the effort for writing complicated custom script.

 

I hope this will solve your problem.  

 

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hey Vishal,

your proposal works like a charm! Excellent.

 

Please allow one additional question:

If I now want to compare with another (second) field, I used the following RefQual:

javascript&colon;"sys_id!=" + current.variables.primary_contact^OR"sys_id!=" + current.variables.secondary_contact;

Unfortunately this does not work.

I also tried:

javascript&colon;"sys_id!=" + current.variables.primary_contact^OR + current.variables.secondary_contact;

which neither works.

Any suggestion?