Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Help with Relationship query for Related List

Oliver Anderson
Kilo Sage

I'm trying to show a Related List on the sys_user table that display's open Incidents submitted by other members of that particular user's department.

I've tried a few different queries, and I think I'm either misunderstanding the way parent and current work in refineQuery() or I'm just doing it wrong.

 

First I tried a simple refineQuery() with just some dot-walking in the current.addQuery():

 

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	current.addActiveQuery();
	current.addQuery(opened_by.department.sys_id, parent.department.sys_id);
})(current, parent);

 

Then I tried getting an array of all the users' sys_ids in the parent's department and comparing opened_by.department to the array. This does something and pares the list down to 160 incidents instead of all 73000 active incidents, but it's not a list of the user's department member's incidents, and I can't find a relation between my query and what the list is showing.

 

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	var deptUsers = [];
	var gr = new GlideRecord('sys_user');
	gr.addQuery('department', parent.department.sys_id);
	gr.query();
	while (gr.next()) {
		deptUsers.push(gr.sys_id);
	}

	current.addActiveQuery();
	current.addQuery(opened_by.sys_id, 'IN', deptUsers.toString());
})(current, parent);

 

 

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Oliver Anderson Please update your script as follows.

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	var deptUsers = [];
	var gr = new GlideRecord('sys_user');
	gr.addQuery('department', parent.department.sys_id);
	gr.query();
	while (gr.next()) {
		deptUsers.push(gr.getValue('sys_id'));
	}

	current.addActiveQuery();
	current.addQuery('opened_by', 'IN', deptUsers.toString());
})(current, parent);

 

View solution in original post

1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@Oliver Anderson Please update your script as follows.

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	var deptUsers = [];
	var gr = new GlideRecord('sys_user');
	gr.addQuery('department', parent.department.sys_id);
	gr.query();
	while (gr.next()) {
		deptUsers.push(gr.getValue('sys_id'));
	}

	current.addActiveQuery();
	current.addQuery('opened_by', 'IN', deptUsers.toString());
})(current, parent);