Using Script add group managers to a specific group

Indup
Tera Expert

Hi all,

There is a group called, Nike Owners. There are four more groups called Nike A, Nike B, Nike C, Nike D.

My requirement is, all the managers of the groups Nike A,B,C,D must populate in Group Members related list of the group Nike owners. Using script i want o achieve this. So should i go with display BR, or something else. Can some one give me script plz

 

Regards,

Indup

1 ACCEPTED SOLUTION

Hi,

@Indup I think this question is still not answered as per your latest comment; so please check

below comments

Are those groups already existing? if yes then currently you will have to manually add the Group Managers of  Nike A, Nike B, Nike C, Nike D to the Group Nike Owners

1) For Existing scenario -> do it manually if less group; if more groups are there then write background script

2) For real-time scenario -> below approach

For making it real-time whenever manager of any of the 4 groups changes you want it to be added and remove the older one

Please use below approach

Business Rule on Group Table

BR Condition: Name is One of those 4 and Manager Changes

find_real_file.png

After Update:

Script: It would get previous manager and current manager

query table and delete record of previous manager and create new record for current manager

var previousManager = previous.manager;

var currentManager = current.manager;

// query Group Member Table

var member = new GlideRecord('sys_user_grmember');
member.addQuery('group', current.sys_id);
member.addQuery('user', previousManager);
member.query();
if(member.next()){

member.deleteRecord(); // this will delete old manager

// now you need to add new manager to this group as member

var member1 = new GlideRecord('sys_user_grmember');
member1.initialize();
member1.group = current.sys_id;
member1.user = currentManager;
member1.insert();

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

15 REPLIES 15

Dhananjay Pawar
Kilo Sage

Hi Indu,

When you want to add those managers in that group?

Hi, Not when. Automatically managers of the groups Nike A,B,C,D must populate in Group members of Nike Owners group. Tomorrow if manger changes, then it should be updated Regards, Indup

Hi,

Create after insert update BR on group table and add condition Manager -> changes.

script -

 

(function executeRule(current, previous /*null when async*/) {

var users = [];
var gr= new GlideRecord('sys_user_group');
gr.addEncodedQuery('name=Flow To Jira^ORname=Developer^ORname=Big Data');//Add your group names inside query i.e your encoded query would be like 'name=Nike A^ORname=Nike B^ORname=Nike C^ORname=Nike D'

gr.query();
while(gr.next()){

users.push(gr.manager.name);

}
gs.info("group array ="+users);
var managers=users.toString().split(',');
for (var i = 0; i < managers.length; i++){

var gr1 = new GlideRecord ('sys_user');
gr1.addQuery('name',managers[i]);
gr1.query();
if(gr1.next())
{

var gr11 = new GlideRecord ('sys_user_grmember');
gr11.initialize();
gr11.user = managers[i];
gr11.setDisplayValue('group', 'Managers group');//Name of Group i.e Nike Owners
gr11.insert();
}

}

})(current, previous);

 

Note- functionality is not fully implemented means it is adding managers in that group even if you change the manager but only problem is it is not removing previous users because we do not have such method to check user id manager or not. upto addition of users it working.

Give a try and let me know.

 

Thanks,

Dhananjay.

Thank you Dhananjay, your code is clear and it is also working. But i tried with code given by Praatiksha first. its also working. 

 

 

Regards,

Indup

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Unsure of your exact requirement for adding it as members I would simply create a Relationship from System Defintion >> Relationships & create something as below.

find_real_file.png

Once done will add it to the Group form as a Related list & then simply modify the Relates list layout to display only Group Managers.

find_real_file.png

 

Output as below.

find_real_file.png