
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2020 05:18 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2020 01:25 AM
Hi,
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
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2020 11:45 PM
Hi Pratiksha,
I tried with your code. its working. But once the manager changes, then old manager should be deleted from the Group Members and must update with new manager. For that, where i need to change the script? can you plz suggest me
Regards,
Indup
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-30-2020 01:25 AM
Hi,
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
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2020 05:48 AM
Hi Indup,
Are these groups in a parent child relationship? If so your BR would look something like the below:
When: After / Insert, Update & Delete
Script:
(function executeRule(current, previous /*null when async*/ ) {
switch (current.operation()) {
case 'insert':
if (current.manager != '') {
addUser(current.manager);
}
break;
case 'update':
if (current.manager != previous.manager) {
removeUser(previous.manager);
addUser(current.manager);
}
break;
case 'delete':
removeUser(current.manager);
}
function removeUser(manager) {
var user = new GlideRecord('sys_user_grmember');
user.addQuery('group', current.parent);
user.addQuery('user', manager);
user.query();
if (user.next()) {
user.deleteRecord();
gs.addInfoMessage(gs.getMessage("Removed previous manager {0} from {1}'s group membership.",[manager.getDisplayValue(),current.parent.getDisplayValue()]));
}
}
function addUser(manager) {
var newuser = new GlideRecord('sys_user_grmember');
newuser.initialize();
newuser.group = current.parent;
newuser.user = manager;
newuser.insert();
gs.addInfoMessage(gs.getMessage("Added {0} to {1}'s group membership.",[manager.getDisplayValue(),current.parent.getDisplayValue()]));
}
})(current, previous);
I've also attached the update set for you so you can directly add it to your environment.
If my reply helped with your issue please mark helpful 👍 and correct if your issue is now resolved. ✅
By doing so you help other community members find resolved questions which may relate to an issue they're having.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2020 05:52 AM
Hi Kieran,
Nope, these tables in the Group table does not have parent child relation ships. Your code will work only if there is a parent child relationship
Regards,
Indup

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2020 05:58 AM
Is there a reason they couldn't be placed into a parent child relationship? if you're going to be adding managers to relating group you should probably look into a proper data structure