Is there any easy way to find whether a related list has rows?

narendrad
Kilo Contributor

I have added some related list to my form and I am trying to find out whether that related list has any rows/results on it from UI Action. Is there any easy way to find it programatically?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

If you know the table that the related list is showing records for, and the field it is using to reference the current record, sure. I'll use change_request with a related list of change_task records on it. chage_task.change_request is the field used to create the related list automatically. That is to say that change_request field on the change_task record is the reference to the change_request table.



So if I want a UI action on change_request to tell me how many change_tasks there are...



var count = 0;


var ga = new GlideAggregate('change_task');


ga.addAggregate('COUNT');


ga.addQuery('change_request', current.sys_id);


ga.query();



if (ga.next())


        count = ga.getAggregate('COUNT');



gs.addInfoMessage('There are ' + count + ' change tasks on this change request');


action.setRedirectURL(current);


View solution in original post

5 REPLIES 5

Counting on an m2m list is basically the same. You just pick which reference field to use and any additional queries. For example, if I want to count users in a group, I can do something like this:

function countGroupMembers(groupName) {

	var count = -1;
	var ga = new GlideAggregate('sys_user_grmember');
	ga.addAggregate('COUNT');
	ga.addQuery('group.name', groupName);
	ga.query();
	
	if (ga.query()) {
		count = parseInt(ga.getAggregate('COUNT'));
	}
	
	return count;
}

This function:

  • Uses a m2m table (sys_user_grmember)
  • Assumes you are passing a group name
  • If no group is found, or an error occurs, you get a -1 as a result