- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 03:40 AM
Hello all,
I have a table in ServiceNow, called "tasks". In this table I have a Reference field to another table called "workers". In the "tasks" list I can see the name of the worker who is doing the task. I am trying to implement a script that queries a specific task and displays the number of tasks for a specific worker. What I have done so far is the following
var tasks = new GlideRecord("tasks");
tasks.addQuery("number","12345");
tasks.addJoinQuery("workers", "name", "Bob");
tasks.query();
gs.info('******* tasks count: ' + tasks.getRowCount());
I have two workers, and for the task with number 12345, Alice has 5 tasks and Bob does not have any. However the query returns 5 for both Alice and Bob.
Any ideas what I am doing wrong?
Thank you
Panos
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:09 AM
Hi @Panagiotis Kons,
you could achieve this by using a GlideAggregate with an orderBy. Update with your reference field name and try this:
var tasks = new GlideAggregate("tasks");
tasks.addAggregate("COUNT");
tasks.groupBy("worker_field");
tasks.query();
while (tasks.next()) {
gs.info(tasks.getDisplayValue("worker_field") + " " + tasks.getAggregate("COUNT"));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:25 AM
Hello,
You can use 'addQuery' statement as a normal GlideRecord, check this useful blog related to GlideAggregate and how it work (there are so many different examples that are similar to your case): https://developer.servicenow.com/blog.do?p=/post/glideaggregate/
☆ Community Rising Star 22, 23 & 24 ☆
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:30 AM - edited 12-21-2023 04:32 AM
If it's only for a specific worker then we don't need the groupBy and can just add an addQuery.
var tasks = new GlideAggregate("tasks");
tasks.addAggregate("COUNT");
tasks.addQuery("worker_field", "worker_sys_id");
tasks.query();
if (tasks.next()) {
gs.info(tasks.getAggregate("COUNT"));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:09 AM
Hi @Panagiotis Kons,
you could achieve this by using a GlideAggregate with an orderBy. Update with your reference field name and try this:
var tasks = new GlideAggregate("tasks");
tasks.addAggregate("COUNT");
tasks.groupBy("worker_field");
tasks.query();
while (tasks.next()) {
gs.info(tasks.getDisplayValue("worker_field") + " " + tasks.getAggregate("COUNT"));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:19 AM
Hello Rene,
thank you for your quick reply. How do I pass the worker name in the aggregate?
Regards
Panos
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:25 AM
Hello,
You can use 'addQuery' statement as a normal GlideRecord, check this useful blog related to GlideAggregate and how it work (there are so many different examples that are similar to your case): https://developer.servicenow.com/blog.do?p=/post/glideaggregate/
☆ Community Rising Star 22, 23 & 24 ☆
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:30 AM - edited 12-21-2023 04:32 AM
If it's only for a specific worker then we don't need the groupBy and can just add an addQuery.
var tasks = new GlideAggregate("tasks");
tasks.addAggregate("COUNT");
tasks.addQuery("worker_field", "worker_sys_id");
tasks.query();
if (tasks.next()) {
gs.info(tasks.getAggregate("COUNT"));
}