- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2022 12:40 AM
1) Groups with no members should include the group manager as a member.
2) Deactivate the group if there are no members or group managers.
Please help me in code.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2022 02:20 AM
Hi @s7
Please find the script to be executed on Background Script or Fix Script to implement it :
var grps = new GlideRecord('sys_user_group');
// grps.addQuery('sys_id','4bccbb5397da51109b1dd200f153aff9'); /Used this sys_id to test for particular group
grps.addActiveQuery();
grps.query();
while(grps.next())
{
//gs.print("grps");
var grmember = new GlideRecord('sys_user_grmember');
grmember.addQuery('group',grps.sys_id);
grmember.query();
if(!grmember.hasNext())
{
// gs.print('grmember');
if(grps.manager)
{
//gs.print(grps.manager);
var grpm = new GlideRecord('sys_user_grmember');
grpm.initialize();
grpm.group = grps.sys_id;
grpm.user = grps.manager.sys_id;
grpm.insert();
} else {
grps.active = false;
}
}
grps.update();
}
IF want to implement it through Business rule or any server side script you can try below script:
var grps = <GlideRecord object of Group value> / may be like current.assignment_group etc.
while(grps.next())
{
//gs.print("grps");
var grmember = new GlideRecord('sys_user_grmember');
grmember.addQuery('group',grps.sys_id); // if returning sys_id in grps variable than can replace 'grps.sys_id' with 'grps'
grmember.query();
if(!grmember.hasNext())
{
// gs.print('grmember');
if(grps.manager)
{
//gs.print(grps.manager);
var grpm = new GlideRecord('sys_user_grmember');
grpm.initialize();
grpm.group = grps.sys_id;
grpm.user = grps.manager.sys_id;
grpm.insert();
} else {
grps.active = false;
}
}
grps.update();
}
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2022 02:47 AM - edited 10-08-2022 02:51 AM
Hi,
Deactivate the group if there are no members or group managers
1. create a one report on sys_user_group table and type of report score card and filter condition is manager is empty and related list condition select Group members related list and Quantity =0. so it will get the groups without manager and no users.
Once you save you will see the count. you can open any of the group all those are without manager and without users.
You can verify and update all the groups to inactive on the list . Click on Active Column and Update All to set Inactive.
Similarly for the Groups with no members should include the group manager as a member.
Create the report like manager is not empty and Group members related list and Quantity =0. If those are less you can open and add user as manager.
If you have to do with the script then check below solution try to create fix and background script for that
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2022 10:42 AM
Hi @s7
Please mark this Solution as Correct Answer if this resolved your queries.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2022 11:15 PM
Hi @s7 ,
I have added code to deactivate Group if No Group member AND Group Manager also missing.
IF you want that Group should be deactivated if Group Manager if absent even though Members are present, you can try below code:
var grps = current.groupField;
if(!grps.manager) // Or can try grps.manager.sys_id
{
grps.active = false;
grps.update();
} else {
var grmember = new GlideRecord('sys_user_grmember');
grmember.addQuery('group',grps.sys_id); // if returning sys_id in grps variable than can replace 'grps.sys_id' with 'grps'
grmember.query();
if(!grmember.hasNext())
{
var grpm = new GlideRecord('sys_user_grmember');
grpm.initialize();
grpm.group = grps.sys_id;
grpm.user = grps.manager.sys_id;
grpm.insert();
}
}
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2022 02:47 AM - edited 10-08-2022 02:51 AM
Hi,
Deactivate the group if there are no members or group managers
1. create a one report on sys_user_group table and type of report score card and filter condition is manager is empty and related list condition select Group members related list and Quantity =0. so it will get the groups without manager and no users.
Once you save you will see the count. you can open any of the group all those are without manager and without users.
You can verify and update all the groups to inactive on the list . Click on Active Column and Update All to set Inactive.
Similarly for the Groups with no members should include the group manager as a member.
Create the report like manager is not empty and Group members related list and Quantity =0. If those are less you can open and add user as manager.
If you have to do with the script then check below solution try to create fix and background script for that
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2024 04:19 AM
Hi,
I just wanted to confirm, should the groups with no members and no managers marked as deactive? Is there any other dependency that one should check for the assignment groups, so as to not break anything?
Thanks!