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

And if you are running it on sc_request table to count all task's under that REQ   as mentioned by goranlundqvist ,which means REQ ---->RITM--->TASK's



Then replacing line 2 with below should work



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



Test and verify


Another thing.



You are using current.sys_id here. that is the sys_id of the Request itself. Which means that looking into the request_item table for catalog task will come up empty, since they all are connected to the requested items sys_id.



If you want to show on the request how many catalog tasks it has, you have to change the line "sctask.addQuery('request_item', current.sys_id);" to


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



This because all the requested item has the request as a parent. Now it will show a message on the request how many tasks it has.


will_smith
Mega Guru

What I am trying to do jeevankj is to get a count of the number of additional tasks and requested items when viewing a particular task. I am getting this information so that I can meet the following business requirements: 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

Here's what I have, which I modified to count both the requested items and tasks when viewing the request. I am not sure where these numbers are coming from, as REQ0000012 only has 1 RITM and 2 TASKS. I am even more confused now...



find_real_file.png



BR Page


find_real_file.png


William,



For the Count of RITM use



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