Simple group member counting

soukypro
Kilo Contributor

Hi Guys,

please could anybody help me with the problem with counting of users in a group?

In table sys_user_group I created new colum ("u_group_members",integer, read only) where I want to calculate users in a group, so on the table I created new business rule for counting users. (configuration attached)

When I use this business role, nothing saved to culumn "u_group_members". Please could tou check my configuration of business rule and a code snippet what I attached and help me to solve this issue?

Thank you!

find_real_file.png

find_real_file.png

(function executeRule(current, previous /*null when async*/) {

var grpm = new GlideAggregate('sys_user_grmember');

  grpm.addQuery('group','=', current.sys_id);

  grpm.addAggregate('COUNT','sys_id');

  grpm.query();

  var groupMembers = 0;

  if(grpm.next())

  {

  groupMembers = grpm.getAggregate('COUNT');

  current.u_group_members = groupMembers;

  current.update();

  }

})(current, previous);

1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

Can you please change this code to



var grp = new GlideRecord('sys_user_grmember');


grp.addQuery("group", current.sys_id);


grp.query();


current.u_group_members = grp.getRowCount();



Do not use current.update() in a before business rule.



If u_group_members is a string field then change the line to current.u_group_members = grp.getRowCount().toString();



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

8 REPLIES 8

Yes it works great, thanks a lot!


Maybe one next question, is there any option, how can I run this rule for all groups without editing the group?


It could be diffrent way than business role


Thanks!


Keep the Business rule active and order as 1000 and try to run it in a background script. We can try updating all groups which should populate it.



var grp = new GlideRecord('sys_user_group');


grp.query();


while(grp.next()){


grp.u_group_members = 0;


grp.update();


}



I think this should work.



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


shloke04
Kilo Patron

Hi,



You can refer the below links which may be useful:



https://www.servicenowguru.com/reporting/group-member-counts-reports/


Find Groups without Group Members — ServiceNow Elite  



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hello Shloke, thanks for response!


Before I created this question I tried to check these posts.


But thank you for your time


PS