The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to count the number of tasks assigned to the group of the logged-in user?

jakelaux
Kilo Explorer

Hello all,

I'm currently working on a script to count the number of tasks assigned to "My Group" based on my groups work. I just need to get a count of the number of tasks and nothing else. I've tried a couple of different things, but this is where the script is currently at:

getTeamTaskCount: function() {

            var user_groups = gs.getUser().getMyGroups();

            gs.log(user_groups);

            var cnt = 0;

            var gr = new GlideRecord('task');

            gr.addQuery('assignment_group', 'IN', user_groups);

            gr.addQuery('active', true);

            gr.addQuery('assigned_to', null);

            gr.query();

            while(gr.next()){

                      gs.log(cnt + ": " + gr.getDisplayValue());

                      cnt= cnt + 1;

            }

  return cnt;

}

This query is updating the count but it is completely off...I think that the query is skipping groups based on the task numbers I've checked.

It would be great if someone could help me out with this issue.

Thanks,

Jake

1 ACCEPTED SOLUTION

edwin_munoz
Mega Guru

Hello Jacob,



Please try this variation of you script:



getTeamTaskCount: function() {


            var user_groups = gs.getUser().getMyGroups();


            user_groups = user_groups.toString().replace('[', '').replace(']', '');


            gs.log(user_groups);


            var cnt = 0;


            var gr = new GlideRecord('task');


            gr.addQuery('assignment_group', 'IN', user_groups);


            gr.addQuery('active', true);


            gr.query();


            while(gr.next()){


                  if(! gr.assigned_to){


                                gs.log(cnt + ": " + gr.getDisplayValue());


                                cnt++;


                  }


            }


  return cnt;


}



Let me know the outcome.



Thanks


View solution in original post

7 REPLIES 7

Hello AKb,



1. It depends on where you want to use the number of tasks, if you only need to know them one time you can run a background script




2. You can use this code:



var reqItem = new GlideRecord('sc_req_item');


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


reqItem.query();


reqItem.getRowCount();



Thanks!


Hi Edwin,



Hope you are well, Thank you very much for your quick response.


I almost have the same script but it doesn't return me the # of RITMS contained into request.


You can find my screenshots into this community : https://community.servicenow.com/message/806573#806573


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



Kind regards


ZA


I want the number of task in order to sent or not emails.


Basically, for all requests and tasks, we send two email (one if the task has been approved/rejected and another one when the request has been completed).



Thus, when the request has only one task and if it is rejected, two emails are sent. we only want to send rejection email.


but if there is two and more tasks, we send two emails (even if all of them have been approved or rejected).



Have you got any idea please ? I look forward to your reply.



Kind regards