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

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


Hello Alikutty,


thanks for your response! I changed the code, but it still doesnt work 😕


Any idea?


Thank you,


PS


This should have worked, Can you please copy the code and try the log that I have added?



var grp = new GlideRecord('sys_user_grmember');


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


grp.query();


gs.log(grp.getRowCount(), "COUNT");


current.u_group_members = grp.getRowCount();



See what count it returns in log.



Thanks


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


Hi Pavel,



I tested and the code was working, When you test you should be modifying the group data like description to run this business rule.



Thanks


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