Schedule job to add/remove users to/from group

Maloy Banerjee
Mega Expert

Hi All,

 

I need your help in the Business application form where I have a user reference field. There is only one filter condition in this field i.e. Active=true

Requirement: A schedule job needs to be created that will compare the Group members of a Group 'xyz' to the complete list of Business Application. It will then add users to the group or remove users from the group based on if or not they are actually defined as the "IT Solution Architect " on Business Applications. The schedule job will run daily.

If selected "IT Solution Architect" is of department that starts with SGRE COG IT, it must be added to APM IT Solution Architect Group, else it should be added to APM OT Solution Architect group.

 Can someone please help on how to proceed.

 

Regards,

Maloy Banerjee

1 ACCEPTED SOLUTION

Hi @Ankur Bawiskar ,

 

Thanks for all the help. I tried creating a flowchart later to make my script simpler (I directly started with the code instead of creating a proper logic) and got the below solution. It worked.

deleteUser();
addUser();

var count;
var remGrp;
var bApp;
var bApp1;
var itsa;
var otsa;
var usrID;
var usr;
var dept;
var grpITSA;
var grpAddITSA;
var grpOTSA;
var grpAddOTSA;
//To delete a User
function deleteUser() {

    remGrp = new GlideRecord('sys_user_grmember');
    remGrp.addEncodedQuery('group=3fc297151b1bd050afc47db4464bcb50^ORgroup=afd111371b1428984016eacee54bcbb8'); //Group: APM IT Solution Architect
    remGrp.query();
    while (remGrp.next())
	{
        bApp = new GlideRecord('cmdb_ci_business_app');
        bApp.addEncodedQuery('it_application_ownerISNOTEMPTY');
        bApp.addQuery('it_application_owner', remGrp.user);
        bApp.query();
        count = bApp.getRowCount();
        if (count == 0)
		{
            remGrp.deleteRecord();
        }
    }
}



//To add a User
function addUser() {
    bApp1 = new GlideRecord('cmdb_ci_business_app');
    bApp1.addEncodedQuery('it_application_ownerISNOTEMPTY');
    bApp1.query();
    while (bApp1.next()) 
	{
        itsa = '3fc297151b1bd050afc47db4464bcb50';
        otsa = 'afd111371b1428984016eacee54bcbb8';
        usrID = bApp1.it_application_owner.toString();
        usr = new GlideRecord('sys_user');
        usr.addQuery('sys_id', usrID);
        usr.query();
        if (usr.next()) 
		{
            dept = usr.department.getDisplayValue();

            if (dept.startsWith("SGRE COG IT")) 
			{
                grpITSA = new GlideRecord('sys_user_grmember');
                grpITSA.addQuery('group', itsa);
                grpITSA.addQuery('user', usrID);
                grpITSA.query();
                if (!grpITSA.next()) 
				{
                    grpAddITSA = new GlideRecord('sys_user_grmember');
                    grpAddITSA.initialize();
                    grpAddITSA.group = itsa;
                    grpAddITSA.user = usrID;
                    grpAddITSA.insert();
                }
            } 
			
			else 
			{
                grpOTSA = new GlideRecord('sys_user_grmember');
                grpAddOTSA.addQuery('group', otsa);
                grpAddOTSA.addQuery('user', usrID);
                grpAddOTSA.query();
                if (!grpAddOTSA.next())
				{
                    grpAddOTSA = new GlideRecord('sys_user_grmember');
                    grpAddOTSA.initialize();
                    grpAddOTSA.group = otsa;
                    grpAddOTSA.user = usrID;
                    grpAddOTSA.insert();
                }
            }
        }
    }
}

 

Regards,

Maloy Banerjee

View solution in original post

7 REPLIES 7

Hi,

if you are getting rowCount as 0 then it won't enter the else condition so it won't delete

Also remove update() wherever you are deleting

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Thanks for all the help. I tried creating a flowchart later to make my script simpler (I directly started with the code instead of creating a proper logic) and got the below solution. It worked.

deleteUser();
addUser();

var count;
var remGrp;
var bApp;
var bApp1;
var itsa;
var otsa;
var usrID;
var usr;
var dept;
var grpITSA;
var grpAddITSA;
var grpOTSA;
var grpAddOTSA;
//To delete a User
function deleteUser() {

    remGrp = new GlideRecord('sys_user_grmember');
    remGrp.addEncodedQuery('group=3fc297151b1bd050afc47db4464bcb50^ORgroup=afd111371b1428984016eacee54bcbb8'); //Group: APM IT Solution Architect
    remGrp.query();
    while (remGrp.next())
	{
        bApp = new GlideRecord('cmdb_ci_business_app');
        bApp.addEncodedQuery('it_application_ownerISNOTEMPTY');
        bApp.addQuery('it_application_owner', remGrp.user);
        bApp.query();
        count = bApp.getRowCount();
        if (count == 0)
		{
            remGrp.deleteRecord();
        }
    }
}



//To add a User
function addUser() {
    bApp1 = new GlideRecord('cmdb_ci_business_app');
    bApp1.addEncodedQuery('it_application_ownerISNOTEMPTY');
    bApp1.query();
    while (bApp1.next()) 
	{
        itsa = '3fc297151b1bd050afc47db4464bcb50';
        otsa = 'afd111371b1428984016eacee54bcbb8';
        usrID = bApp1.it_application_owner.toString();
        usr = new GlideRecord('sys_user');
        usr.addQuery('sys_id', usrID);
        usr.query();
        if (usr.next()) 
		{
            dept = usr.department.getDisplayValue();

            if (dept.startsWith("SGRE COG IT")) 
			{
                grpITSA = new GlideRecord('sys_user_grmember');
                grpITSA.addQuery('group', itsa);
                grpITSA.addQuery('user', usrID);
                grpITSA.query();
                if (!grpITSA.next()) 
				{
                    grpAddITSA = new GlideRecord('sys_user_grmember');
                    grpAddITSA.initialize();
                    grpAddITSA.group = itsa;
                    grpAddITSA.user = usrID;
                    grpAddITSA.insert();
                }
            } 
			
			else 
			{
                grpOTSA = new GlideRecord('sys_user_grmember');
                grpAddOTSA.addQuery('group', otsa);
                grpAddOTSA.addQuery('user', usrID);
                grpAddOTSA.query();
                if (!grpAddOTSA.next())
				{
                    grpAddOTSA = new GlideRecord('sys_user_grmember');
                    grpAddOTSA.initialize();
                    grpAddOTSA.group = otsa;
                    grpAddOTSA.user = usrID;
                    grpAddOTSA.insert();
                }
            }
        }
    }
}

 

Regards,

Maloy Banerjee

Thanks for the update

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader