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

will_smith
Mega Guru

goranlundqvist, if you have 5 more minutes, would you mind taking a look at this for me? All of this was to work toward this requirement that I was given: cancel the current task if there is only one task and one request item; if there is +1 Task or +1 Requested item, only cancel the current task.



The other issue I am running into is setting the rejected state of the request from the catalog task. If you're willing to assist with this too I would really appreciate it. I've been working on this one for far too long. Here's the code for that... but it's failing on the same addQuery line as this issue. I think the original post was here: Updating a change record from the change task record



function updateREQApproval() {
  var reqst = current.request;
  var req = new GlideRecord('sc_request');
  req.addQuery('sys_id', reqst); //REQ sys_id
  req.query();
  if (req.next()) {
  gs.log('req number = ' + req.number);
  req.request_state = 'closed_cancelled';
  req.approval = 'rejected';
  req.update();
  gs.addInfoMessage('Request, Requested Item(s), and Task(s) have all been closed.');
  }
}


will_smith
Mega Guru

I solved it with the following code. now to implement this into my cancel & comment UI   Page processing script...



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



gs.flushMessages(); //clears all previous Info Messages



var ritms = new GlideAggregate('sc_req_item');


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


ritms.addAggregate('COUNT');


ritms.addActiveQuery();


ritms.query();



var sctask = new GlideAggregate('sc_task');


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


sctask.addAggregate('COUNT');


sctask.addActiveQuery();


sctask.query();


if (current.request.getDisplayValue() != "") { //if the request number isn't blank


if (ritms.next() && sctask.next()) {


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


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


}


}


}


Ahh,



Didn't know there was "request" field on the catalog tasks. I see now that there is and then of course it's easier to refer to it than to got through parent.parent.. etc 😃




For your question about addQuery if we look at "ritms.addQuery('request', current.request.sys_id);"


here you say that you are looking for the field 'request' and it should have the value of current.request.sys_id.



And the line above "var ritms = new GlideAggregate('sc_req_item');" so it know that it is in the table sc_req_item it should look for the field.



Still need help with the UI or you figured that out? The rest of the family is finally asleep now, so got some spare time over


will_smith
Mega Guru

I do still need help with that UI issue, if you have some time. I laid it out in a new thread Cancel and comment client UI action not working...



Would you be willing to do some sort of a screen share with me? Send me an email at william.smith3@gmail.com if you're ok with this and we can set something up, hopefully in the next 24 hours. I am in the US, on Eastern time.


I'll send you an email about it.