The CreatorCon Call for Content is officially open! Get started here.

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

I would try this:



Replace line 4 with:


reqItems.addQuery('parent',current.sys_id);



Line 13


taskItems.addQuery(request_item.parent, current.sys_id);


I did like I posted erlier and got this now.



display br tasks count 2.GIF



With this code in the BR:



(function executeRule(current, previous /*null when async*/) {



  var sctask = new GlideAggregate('sc_task');


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


  sctask.addActiveQuery();


  sctask.addAggregate('COUNT');


  sctask.query();


  if (sctask.next()) {



  gs.addInfoMessage('Catalog tasks :   ' + sctask.getAggregate('COUNT'));


  }



  var ritms = new GlideAggregate('sc_req_item');


  ritms.addQuery('parent', current.sys_id);


  ritms.addActiveQuery();


  ritms.addAggregate('COUNT');


  ritms.query();


  if (ritms.next()) {



  gs.addInfoMessage('Requested items:   ' + ritms.getAggregate('COUNT'));


  }



})(current, previous);


will_smith
Mega Guru

Thanks. I am going back and forth between using the Catalog Request [sc_request] table and the Catalog Task Table as my GlideAggregate query. Should I be using the same table in the first line var foo = new GlideAggregate('sc_task') in the requested items search and the tasks search?



Here's my code now: and it returns 0 for both values, when ran from the Catalog Task table. Can I search both up the chain and down REQ ->RITM ->TASK?



(function executeRule(current, previous /*null when async*/) {



  var reqItems = new GlideAggregate('sc_req_item');


  reqItems.addQuery('request', current.sys_id);


  reqItems.addActiveQuery();


  reqItems.addAggregate('COUNT');


  reqItems.query();


  if (reqItems.next()) {


  gs.addInfoMessage('Requested Items: ' + reqItems.getAggregate('COUNT'));


  }



  var taskItems = new GlideAggregate('sc_task');


  taskItems.addQuery('request_item.request', current.sys_id);


  taskItems.addActiveQuery();


  taskItems.addAggregate('COUNT');


  taskItems.query();


  if (taskItems.next()) {


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


  }



})(current, previous);


Se my example above where I "DOT walk" on line 13.



I also noticed I missed the '' on the 'request_item' on line 13.


This works in my system when ran from sc_request.



find_real_file.png