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

dmfranko
Kilo Guru

What's your actual BR code look like?


manikorada
ServiceNow Employee
ServiceNow Employee

William,



I would use the business rule something like this:



var sctask = new GlideRecord('sc_task');


sctask.addQuery('request_item', current.sys_id);


sctask.addActiveQuery();


sctask.query();


gs.addInfoMessage('Tasks :   ' + sctask.getRowCount());


Shouldn't the BR look something like:



var sctask = new GlideRecord('sc_task');


sctask.addQuery('request_item', current.sys_id);


sctask.addActiveQuery();


sctask.query();


if (sctask.next()) {


gs.addInfoMessage('Tasks :   ' + sctask.getRowCount());


}



Seems you forgot the IF-statement.



I threw the above code in a BR display for sc_req_item table and got this:



display br tasks count.GIF


will_smith
Mega Guru

Thank you everyone. Is it possible to perform this with GlideAggregate instead of GlideRecord?