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

incase you missed the post above.



There is the problem.



The table in the BR if for when the BR should run. and since you want it to run when you open a request, it should point to table sc_request


I had wanted it to run in the Catalog Task table since that's what the helpdesk has access to, and works out of.



Let's see if this helps.... I ran this in my BR and got "Catalog tasks: 2; Requested items: 1", so we know the code works. it's must be something to do with finding that request sys_id, but everyone says it's working.


find_real_file.png


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


//REQ0000004 = c42666440a0a0b12002075c48bc98a56



  var sctask = new GlideAggregate('sc_task');


  sctask.addQuery('request_item.parent', 'c42666440a0a0b12002075c48bc98a56');


  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', 'c42666440a0a0b12002075c48bc98a56');


  ritms.addActiveQuery();


  ritms.addAggregate('COUNT');


  ritms.query();


  if (ritms.next()) {



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


  }



})(current, previous);


Now lets see.



So the HelpDesk only looks at the catalog tasks and when clicking on a specific task, they want to see how many tasks & RITMS it's Request has.



Then we do like this.



We have replaced current.sys_id with current.parent.parent.sys_id.



Since we want to know what request is connected to this task we have to do some dot walking.


First thing is that we want to look at our current task.


so we begin with "current."



Then it looks at its parent which is the RITM.


So we add parent to the string "current.parent".



Then we look at the RITM's parent which is the Request


So we add another parent to the string "current.parent.parent".



Now we are inside the request record and we want the sys_id.


So we add sys_id to the string "current.parent.parent.sys_id"



Hope this is making sense to you, but it is easy to get lost in the dot walking, so if something isnt working, make sure you got the correct string of dot walking.





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



  var sctask = new GlideAggregate('sc_task');


  sctask.addQuery('request_item.parent', current.parent.parent.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.parent.parent.sys_id);


  ritms.addActiveQuery();


  ritms.addAggregate('COUNT');


  ritms.query();


  if (ritms.next()) {



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


  }



})(current, previous);


I think I may have just found an issue when I move this from the DEMO environment to our DEV environment. It's giving me the total number of tasks and requested items (I think?) as I have a task that has one RITM and one TASK and I'm getting Requested items: 1061.



I will keep working, but if you have any quick ideas I am open to suggestions.


will_smith
Mega Guru

It works!!!!! Thank you, thank you!!!



That makes sense to me, and I had tried that about 10 mins ago - but it gave me all 0's and so I just kept writing/trying different things. I was probably making it worse.



The whole thing hinges on the   ritms.addQuery('parent', current.parent.parent.sys_id); line. I had noticed that jeevankj had used 'request' instead of 'parent' - does that make a difference?



What objects are being called in the first part of this addQuery in the GlideRecord?