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

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);


richelle_pivec
Mega Guru

Chuck, is there a way to use (part of) this script to populate a field on the form with the count of the related records? For me it's KBs (m2m_kb_task) related on the Incident form. I'd like to have a count of the KBs in a field called u_kb_count that lives on the Incident Form.

I tried a Client-side, Before Business rule like this, but it did not work:

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
    
       var count = 0;

var ga = new GlideAggregate('m2m_kb_task');

ga.addAggregate('COUNT');

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

ga.query();


if (ga.next())

         count = ga.getAggregate('COUNT');
    
g_form.setValue('u_kb_count', count);


})(current, previous);

 

Thanks,

Richelle

Hi Richelle

Did you ever get a response on this? Im trying to do a BR count on a m2m related list as well

Steve

No, I'm sorry to say I did not find a way to do this. In the end, I just added another field on the incident form requiring the team member who resolves to validate that the knowledge attached was correct, incorrect, needed updating, or wasn't available.

An ugly solution, but it also gives us a way to find knowledge that needs updating, creating or a Service Desk Analyst that need education.

Richelle