group with no member

s7
Tera Contributor

 

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.

2 ACCEPTED SOLUTIONS

AnubhavRitolia
Mega Sage
Mega Sage

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();
}

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

Pavankumar_1
Mega Patron

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.

Screenshot (185).png

 

You can verify and update all the groups to inactive on the list . Click on Active Column and Update All to set Inactive.

Screenshot (186).png

 

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

https://www.servicenow.com/community/it-service-management-forum/using-script-add-group-managers-to-...

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

8 REPLIES 8

Hi @s7 

 

Please mark this Solution as Correct Answer if this resolved your queries.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

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();
        }
    }
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Pavankumar_1
Mega Patron

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.

Screenshot (185).png

 

You can verify and update all the groups to inactive on the list . Click on Active Column and Update All to set Inactive.

Screenshot (186).png

 

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

https://www.servicenow.com/community/it-service-management-forum/using-script-add-group-managers-to-...

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

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!