Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

zica
Giga Guru

Hello every one,



I finally found the solution of my problem. Thanks a million for your help.

What I did :


In the beginning I count the number of RITMS contained into the request. If there is only one, I check the stage (not the state) of the RITMS. If rejected, the stage would be Cancelled (the state remains approved).


Since there, I give the value true or false to the global variable answer.



For those who may are/ will have the same problem, you will find the script below (don't feel shy to mark helpful/correct answer or just like it )



var count = 0;


var reqItems = new GlideAggregate('sc_req_item');


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


reqItems.addAggregate('COUNT');


reqItems.query();


while (reqItems.next()) {


        count = reqItems.getAggregate('COUNT');


}



if (count == 1) {


            var gr = new GlideRecord('sc_req_item');


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


            gr.query();


            if (gr.next()) {


                      gs.addInfoMessage('Stage of RITM found is : ' + gr.stage.toString()); // this row checks the stage of RITMS


            }


            if (gr.stage.toString().indexOf('Cancelled') != -1) {


                      answer = false;


            } else {


                      answer = true;


            }


} else {


            answer = true;


}




Kind Regards;


ZA