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

Answer = true means that it will send it.



Use an Advanced condition to send a notification based on the current email record, changing field values, or system properties. To send the notification, your advanced condition script must set a global answer variable to true.


As Berny told, it will send if the aswer is set to true and it will not when answer is set to false.


zica
Giga Guru

Hello,


Thank you for your reply and Help,I have tested your script, but I dont understant it is not working...



Case number 1 : When the request contains only one RITMS, and IF the RITMs is rejected, the RITM state will change to 'close completed'. At this state,   the "rejection Email" will be sent (PICTURE NUMBER 1). And then, the system will change the request State to ' Close completed'. There is also a "survey email" already setted up, but I dont want to send it. I only want to send the last Email (survey Email) in the case Number 2, Which is :



Case number 2 : If the request has more than 1 RITM (no matter if some of them have been rejected and/or approved). in other words, in this case, both rejection and survey email will be sent.



you will find my screenshots above : I ONLY TRIED to send the SURVEY email if there is MORE THAN 1 RITMS into the Request (PICTURE NUMBER 2). The script is executed into 'ADVANCED CONDITION' of 'SURVEY email' (PICTURE NUMBER 3).



But it is NOT working.




PICTURE NUMBER 1 : Requested ITEM LEVEL :


ReqItem level.PNG


PICTURE NUMBER 2 : REQUEST LEVEL, HAS GOT 2 RITMS :


RITMS.PNG




PICTURE NUMBER 3 : ADVANCED CONDITION OF SURVEY EMAIL



requested_completed.PNG




If you have any advice for me, I am more than interested,




Thank you very much for your help,




Kind Regards,


ZiA


Hi ZiA,



Remove the first function line and the last line with the curly bracket and give it a try.



Thanks,


Berny


Hi AKb,



First thing you are doing wrong is you are re-declaring the answer variable with set to false. This will always make the advanced condition false. As soon as you set the answer variable it returns from there making the advanced condition false/true depending on your value assignment.


Its a global variable declared by ServiceNow. You don't need to declare it again. Please see the code I have shown in the screenshot below.



If you are declaring a declaring count variable to store the no of RITMs then why are you not using it in the if condition of "reqItems.getAggregate('COUNT')". Please use count variable here.



Why are you writing the code inside a function? And if you are writing it in a function then call the function above its definition. From nowhere you are calling your function.


So remove the code from function and write it directly.



untitled.png



Please use the same code as I have shown above with two changes, one at line 1 and other at line 11.


Change line 1 to "var count = 0;".


Change line 11 to "if(count == 1){"



Please write the code as it is with the above changes and try.



Thanks,


Tanaji