Get related list row count

Kim Jenson
Kilo Contributor

Hello Experts! 

I am trying to get a row count from a related list and populate a field on the form. Is this possible? For example: I need to get a total count of Catalog Items (32 in this case) in the below screen shot of the 'sc_cat_item_category' related list, and place the value in the 'u_cat_total' on the 'sc_category' table.

Please help!! Thanks! 

find_real_file.png

1 ACCEPTED SOLUTION

So in the 'Calculated' script, you could put the following:

(function calculateValue(current) {
var categoryCount = 0;
var gr = new GlideAggregate('sc_cat_item_category');
gr.addQuery('sc_category', current.sys_id);
gr.addAggregate("COUNT");
gr.query();
while(gr.next()) {
categoryCount = gr.getAggregate("COUNT");
}
return categoryCount;
}(current);

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

13 REPLIES 13

I actually wasn't sure if Calculated field ran on a list. Couldn't find any it mentioned in the documentation!

Good to know.

 

So essentially, both BR and Calculated Fields are valid solutions, both with pros and cons. Choose whatever solution best fits your situation.

 

 

 

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

So coming back to this from a week ago, the solution worked fine, though in the list view, not able to filter on it. I wonder if this was set up as a BR, would that make any difference? 

You can only filter on it if the value is stored after calculation. That only happens when the record has been updated/saved in some way. If the field was created (with the calculation) after the record was last updated, then no value is stored and you won't have anything to query.

 

Hope that makes sense.

Thanks Chuck for the explanation. On an unrelated issue, I would love to get your thoughts on this Question I posted yesterday, which I'm stumped on. It's a time pressing issue trying to figure out a solution so any ideas would be great.

Here's the link: https://community.servicenow.com/community?id=community_question&sys_id=515bcba7db691f002d1efb651f961906

Thanks for your help! Much appreciate it.