How to count the number of task assigned to a request number ?

zica
Giga Guru

Hello There,

I started to work on servicenow recently and I am stuck. I really need your help. I have one question about the topic above.

-> I want to count the number of tasks for each request when the user click in the request row to open the request.

Let me explain : Le'ts say for example that the request number REQ001 has got 3 tasks (RITM0021, RITM002, RITM003). When the user click on REQ001, I want to collect that there is 3 tasks assigned to this request number.

This is the script I have written into UI ACTIONS, but It's not working :

var reqItems = new GlideAggregate('sc_req_item');

reqItems.addQuery('current.number');

reqItems.addAggregate('COUNT');

reqItems.query();

if (reqItems.next()) {

        alert('task count: ' + reqItems.rows.length);

}

Thank you so much for your help

1 ACCEPTED SOLUTION

Vimal Priya
Giga Guru

Zic,


Its working fine in my instance.


Try doing in your instance.


Mark the answers helpful and correct .


View solution in original post

30 REPLIES 30

No thats not a mistake thats intentional.


If we will use while loop then it will display the same value multiple times.


He just wants the Message once giving the count.


Ah true. . My bad.


AKb Zic, would you mind sharing the latest code that you're using?



It does appear that the code from Tanaji Patil, should be working. Just make sure it's the same (querying the sc_req_item table) and having it in a display business rule on the sc_request table.



var reqItems = new GlideAggregate('sc_req_item');


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


reqItems.addAggregate('COUNT');


reqItems.query();


if (reqItems.next()) {


  gs.addInfoMessage('task count: ' + reqItems.getAggregate('COUNT'));


}



Thanks,


Berny


zica
Giga Guru

Hello everyone,


Hope you are well.


There is something wrong going in my instance. I have followed the same steps as you mentionned but still have the same message. Please find below my Screenshots.



1. When to run the business rules


2. The script you have given to me


3. The result of the script. It returns me "Task count 1" whereas there is two RITMS.


BusinessRulesWhenToRun.PNG


BusinessRulesAdvanced.PNGRequestedItemsResult.png



Kind Regards,


ZA


Into servicenow dev instance, I have tested the script with the same request number (REQ000002). You will find enclosed what I had.


I think it's not about the script. You may have checked/put some conditions that I have not.


serviceNowDevTest.PNG


Futhermore, I am querying the sc_req_item table, and having it in a display business rules on the sc_request table.


To be able to find the script >> Business Rules (application navigator) >> TEST_RITM



If you have any advice, I am more than interested.


Thank you,


ZA