- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 03:41 AM
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!
(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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 03:46 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 04:49 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 05:12 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 03:52 AM
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
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 04:14 AM
Hello Shloke, thanks for response!
Before I created this question I tried to check these posts.
But thank you for your time
PS