
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2023 07:34 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 05:30 AM
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:"sys_id!=" + current.variables.primary_conatct + "^sys_id!=" + current.variables.secondary_contact;
Hope this will work for you..!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 06:13 AM
Hello @Jens Koellner,
Use the below scripts to achieve the functionality as mentioned:
Client Script: -
Script Include:-
removeUser: function() {
var retObj = {
"message": "",
"value": "false",
"validSysIds": ""
};
var primary_contact = this.getParameter("sysparm_primary");
var additional_contact = this.getParameter("sysparm_additional");
var duplicateNames = [], validUsers = [];
var approvers = additional_contact.split(",");
for(var i = 0; i < approvers.length; i++) {
var userRef = new GlideRecord("sys_user");
userRef.addQuery("sys_id", approvers[i]);
userRef.query();
while(userRef.next()) {
if (primary_contact == approvers[i]) {
duplicateNames.push(userRef.name);
}
else {
validUsers.push(userRef.getUniqueValue());
}
}
}
if (duplicateNames.length > 0) {
retObj.value = "true";
retObj.message = "Primary Contact and Additional Approvers must be different persons. The following names are duplicated: " + duplicateNames.join(", ");
retObj.validSysIds = validUsers;
}
return JSON.stringify(retObj);
},
If my answer solves your issue, please mark it as Helpful and Accepted based on impact.
Goodwill,
Anshul