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

AnubhavRitolia
Mega Sage
Mega Sage

Hi @s7  

 

I hope you want to run Background Script or Fix Script and implement this for all Groups present in system. OR are you looking to implement it for particular Group?

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 @AnubhavRitolia 

 

I want to implement this for all groups present in system, not for a particular group.

Hi @s7 

 

Already provided solution for both cases in my solution provided earlier. Just check once.

 

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

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