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

Hey Vishal,

as I am not sure whether my first reply reached you ...

--

You proposal works like a charm, excellent!

This resolved my first inquiry.

 

May I kindly ask you to have a look into my extended question:

If I know have a third field/variable "secondary_contact" and I want to resolve the comparison with an extended Reference Qualified expression, how would this look like?

 

Summary:

3 variables/fields in the CatItem form.

  • to compare each single name contained in "additional_approver" with the name in "primary_contact" AND "secondary_contact"
  • If they are the same,
    • A message shall appear outlining that this particular user name has been used for "primary_contact" OR "secondary_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

Any help or suggestion is pretty welcome.

 

Thanks, Jens

 

 

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

Hey Vishal,

this works out!

Thanks a lot for your support.

 

Kind regards,

Jens

--

ps. one thing: if someone wants to circumvent the cross-check .. it is possible to change primary or secondary_contact after you put in a different name into the additional_approver field. But that's a different story.

Yeah indeed that will be the pain-point as we don't know which field user will select first.

For this it will be better to

1. Make "additional approvers" editable only if both primary and secondary approver are selected.

2.Clear value of "additional approvers" if user changes the primary or secondary approver.   

 

not best possible solution but it will work.

Vishal Birajdar
ServiceNow Developer

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

Thanks Vishal. This reference qualifier really saves complex script approach.