- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2018 06:37 PM
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!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2018 08:34 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2018 05:24 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2018 04:32 PM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2018 07:56 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2018 07:00 AM
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.