count the number of catalog tasks in a request

will_smith
Mega Guru

I have read through all of this post How to count the number of task assigned to a request number ?   and the original request was for a count of the number of requested items; I am trying to figure out the proper code for a count of the number of catalog tasks. Can someone help me with this please! I am running out of time on this and my boss is getting antsy.

I have been testing this background scripts and it keeps giving me 31 active tasks - the total # of active tasks, which tells me it's accessing the right table. But when I transfer it to my display BR, based on the post, it's returning "Tasks: 0" every time, why?

var taskItems = new GlideAggregate('sc_task');

  taskItems.addQuery('task','c4288fd50a0a0b1200577058fffbf9a3'); //Request sys_id for RITM0000010

  taskItems.addAggregate('COUNT');

  taskItems.addActiveQuery(); //only counts the active items

  taskItems.query();

  if (taskItems.next()) {

  gs.print('Tasks: ' + taskItems.getAggregate('COUNT'));

  }

42 REPLIES 42

Your code will work with glide aggregate.



You have to replace 'task' with 'request_item'



taskItems.addQuery('request_item','sys_id')     //sys_id of item


will_smith
Mega Guru

Sorry, I should have added a bit more detail. I am running this from the Request [sc_request] table and finding that it's not showing the info message anymore. Any additional help is greatly appreciated.


will_smith
Mega Guru

I am new to the scripting environment in ServiceNow, so let me see if I understand this. On line one I am creating/preparing the glide aggregate query on the Catalog Task table [sc_task]. Then I am adding a query on the table for the request_item's sys_id. Counting the aggregate, showing only the inactive items (lines 3, 4 respectively). Then executing the query.



Am I correct in my assumption that on line 2 the 'request_item' has to match the table that the BR is running from?


Hmmm..





Kind of lost me here.



You want that when people looks at a request. It will show how many catalog tasks it got? Then you can't use the current.sys_id in the query since the request isnt directly connected to the catalog task. Its connected to the requested items that got the connection to the tasks.



But if it is as above, it's easy. Just wanna check first if i understand u correct


So ,based on the initial post you are querying the catalog task sc_task table to   read all active tasks for a particular item.So you are actually counting the active tasks for that particular RITM.   You are correct till this point if I understood your requirement properly.



On Line 2:


No,it doesnt have to match the table where your BR is running,It can run from Request Item table or from other table(depends on your exact requirement )..



For addQuery ,it is the field that your are querying.here request_item is the field on the sc_task form.



Using GlideRecord to Query Tables - ServiceNow Wiki



taskItems.addQuery('request_item','c4288fd50a0a0b1200577058fffbf9a3'); //Request sys_id for RITM0000010  




Request_item is the field which stores the parent RITM for the catalog tasks.Which means you are searching the sc_task table for Parent RITM that is   your RITM -> RITM0000010 sys ID