How populate related list records as a reference field in a Catalog Item

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2022 07:58 PM
Hi Experts
I need to create a User Reference field (could be a lookup select box too), where I should list only the users that comes from a related list in the user record.
This related record is showing all the users reporting under the current user.
So, the Catalog Item form should show all those user to pick one.
In this example, 563 users reports directly or indirectly to the current user. I need the Catalog Item from to show all these users in a reference variable named "Staff" for the Requested For user.
I am not super expert in the scripting. I revised the relationship record
Named "Users Reporting Under:"
(function refineQuery(current, parent) {
// Add your code here, such as current.addQuery(field, value);
var listOfUsers = new UsersReportingTo();
var answer = listOfUsers.report(parent.sys_id);
current.addQuery('sys_id','IN', answer.toString());
})(current, parent);
UsersReportingTo Script Include:
var UsersReportingTo = Class.create();
UsersReportingTo.prototype = {
report:function(userSys_id){
var Mainlist = [];
var UsersList = [];
var gr = new GlideRecord("sys_user");
gr.addQuery("active",true);
if (userSys_id.toString().length == 32)
{
gr.addQuery("manager", userSys_id);
}
else
{
gr.addQuery("manager.name", userSys_id);
}
gr.query();
while (gr.next()) {
UsersList.push(gr.sys_id.toString());
}
Mainlist = UsersList;
do{
var gr1 = new GlideRecord("sys_user");
gr1.addQuery("manager", "IN", UsersList);
gr1.query();
UsersList = [];
while (gr1.next()) {
UsersList.push(gr1.sys_id.toString());
Mainlist.push(gr1.sys_id.toString());
}
}while(UsersList.length>0);
return Mainlist;
},
type: 'UsersReportingTo'
};
Yet, I can't figure out how to list all those users in the reference field using a reference qualifier.
I tried to set a refqual: javascript: new global.UsersReportingTo().userSys_id(current.variables.requested_for);
That didn't work I get ALL sys_users users.
Any help would be greatly appreciated.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2022 08:11 PM
Hi,
It appears that the method you are calling is incorrect.
Try:
javascript: new global.UsersReportingTo().report(current.requested_for);
Hope this helps!
Tudor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2022 08:14 PM
Hi,
So what debugging did you perform by adding gs.info() statements?
regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2022 09:09 PM
Hi Ankur,
Thanks for your reply.
The issue is not with the Script Include, it works fine, as you can see the Related List DOES shows the records. I put them in my post as informational reference (so that you can see how related list records are generated) along with the relationship script.
My issue is that I can't get those related records listed under a reference field un a catalog item.
In the example above, Users Reporting under for the current user shows 563 records, I need that my new reference variable in the catalog item shows only those 563 users when I pick the user where this related records shows under. If I were to be that user, then the 563 records should show in the Staff variable if Requested For was me.