Bothered by Group Types and Adding Roles? (Auto Add Roles by Group Type)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2017 08:28 PM
Hey Folks,
If you are like me and are tired of the tedious tasks, tired of trying to remember everything about everything, i have a little bit of help for you when it comes to groups and the Group Types.
Have you ever made a Group and itil Group and saved the record, and forgot to add the itil role to the group? Yeah. we've all been there (if you're like me.)
We have a bunch of Group Type's and some of those group types need to have roles that go along with them.
So i have a solution. A set it and forget it thing.
On your Group Type table: sys_user_group_type, add 1 field or 2 fields, your choice.
All that is there is the Name and Description.
Create a true/False field (Optional Field) but this way you can turn it off later if you want.
Label: Auto Apply Role
Name/Value: u_auto
Default: true
2nd field:
Type: Reference
Label: Role
name/Value: u_role
Reference: role(sys_user_role)
Now, go into your records and add roles as necessary.
NEXT:
Create a script include:
Name: GroupHelper
Accessible From: All Application Scopes
Active: True
Script:
var GroupHelper = Class.create();
GroupHelper.prototype = {
initialize: function() {
},
removedGroups: function(previousType, currentType) {
var removedArray = [];
var prevArray = previousType.toString().split(",");
for (var i=0; i< prevArray.length; i++) {
if(currentType.toString().indexOf(prevArray[i]) == -1){
removedArray.push(prevArray[i]);
}
}
return removedArray.toString();
},
addedGroups: function(previousType, currentType) {
var addedArray = [];
var curArray = currentType.toString().split(",");
for (var i=0; i< curArray.length; i++) {
if(previousType.toString().indexOf(curArray[i]) == -1){
addedArray.push(curArray[i]);
}
}
return addedArray.toString();
},
type: 'GroupHelper'
};
NEXT:
Create a Business Rule:
Name: Add/Remove Roles by Group Type
Table: Group (sys_user_group)
Active: true
Advanced: true
When: Before
Insert: true
Update: true
Filter Conditions: Type, Changes
Script:
(function executeRule(current, previous /*null when async*/) {
var previousType = previous.type;
var currentType = current.type;
var removed = new GroupHelper().removedGroups(previousType, currentType);
var added = new GroupHelper().addedGroups(previousType, currentType);
if(!added.nil()){
var addedArray = added.split(",");
for (var a=0; a < addedArray.length; a++) {
var groupType = new GlideRecord("sys_user_group_type");
groupType.get(addedArray[a]);
var auto = groupType.u_auto;
var grRole = groupType.u_role;
if(grRole != ''){
//gs.addInfoMessage("Group Role/Auto: " + grRole + " - " + auto);
var GroupHasRole = new GlideRecord("sys_group_has_role");
GroupHasRole.addQuery('group', current.sys_id);
GroupHasRole.addQuery('role', grRole);
GroupHasRole.query();
if (!GroupHasRole.next()){
if (auto == true){
GroupHasRole.group = current.sys_id;
GroupHasRole.role = grRole;
GroupHasRole.inherits = 'true';
GroupHasRole.insert();
gs.addErrorMessage( "***ADDED*** " + grRole.getDisplayValue().toUpperCase() + " - ROLE HAS BEEN ADDED TO THE GROUP BASED ON THE GROUP TYPE: " + groupType.name.toUpperCase() + " - See group type table - " + " ***ADDED***");
}
}
}
}
}
if(!removed.nil()){
var removedArray = removed.split(",");
for (var i=0; i< removedArray.length; i++) {
var groupType1 = new GlideRecord("sys_user_group_type");
groupType1.get(removedArray[i]);
var auto1 = groupType1.u_auto;
var grRole1 = groupType1.u_role;
if(grRole1 != ''){
var GroupHasRole1 = new GlideRecord("sys_group_has_role");
GroupHasRole1.addQuery('group', current.sys_id);
GroupHasRole1.addQuery('role', grRole1);
GroupHasRole1.addQuery('inherits', 'true');
GroupHasRole1.query();
if (GroupHasRole1.next()){
if (auto1 == true){
GroupHasRole1.deleteRecord();
gs.addErrorMessage("***REMOVED*** " + grRole1.getDisplayValue().toUpperCase() + " - ROLE HAS BEEN REMOVED FROM THE GROUP BASED ON THE GROUP TYPE: " + groupType1.name.toUpperCase() + " - See group type table - " + " ***REMOVED***");
}
}
}
}
}
})(current, previous);
Save that, Now test it out!
Now, Please forgive the Code, it may not be a "Best Practice" code, but it works and gets the job done.
I'm interested to hear comments and other ideas for this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2017 04:22 AM
Hey Steven,
Very well done. Thanks for sharing!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2017 10:56 AM
So i was asked if i could write this for a List Collector.
There may be a demand for adding MULTIPLE roles for one group type.
So i have done this
UPDATE: 11/1/2017
Instead of using a Reference field as stated above.
Create new Field
Type: List
Label: Roles
name/Value: u_roles
Reference: role(sys_user_role)
the Same Script Include will work from above.
Create a new Script Include.
Do not use the one above.
Name: Add/Remove Roles by Group Type (Multi)
Table: Group (sys_user_group)
Active: true
Advanced: true
When: Before
Insert: true
Update: true
Filter Conditions: Type, Changes
Script:
(function executeRule(current, previous /*null when async*/) {
var businessRule = 'Business Rule: Add/Remove Roles by Group Type';
// Add your code here
var previousType = previous.type; //Get the previous type field before anything was added/removed from this udpate
var currentType = current.type; //Get the current field value
//pass the current and previous values to a script include to get the groups that have been added/removed.
var removed = new GroupHelper().removedGroups(previousType, currentType);
var added = new GroupHelper().addedGroups(previousType, currentType);
//set font for use in Alert
var font = '<br/><br/><font style="font-size:16px">';
//If any groups are added, run this
if(!added.nil()){
//get the groups in an array and split them to individual sys_id's
var addedArray = added.split(",");
//cycle through the sys_id's
for (var a=0; a < addedArray.length; a++) {
//get the group type record by sys_id
var groupType = new GlideRecord("sys_user_group_type");
groupType.get(addedArray[a]);
//See if there is a role and if it should auto apply.
var auto = groupType.u_auto;
var grRole = groupType.u_roles;
//if there is a role, run this
if(grRole != ''){
//check to see if the group already has the role, If not, add the role
if (auto == true){
var multiRoles = grRole.split(",");
for (var b=0; b< multiRoles.length; b++) {
var GroupHasRole = new GlideRecord("sys_group_has_role");
GroupHasRole.addQuery('group', current.sys_id);
GroupHasRole.addQuery('role', multiRoles[b]);
GroupHasRole.query();
if (!GroupHasRole.next()){
GroupHasRole.group = current.sys_id;
GroupHasRole.role = multiRoles[b];
GroupHasRole.inherits = 'true';
GroupHasRole.insert();
}
}
//Add alert to notify of group role
gs.addErrorMessage(font + "***ADDED*** " + grRole.getDisplayValue().toUpperCase() + " - ROLE HAS BEEN ADDED TO THE GROUP BASED ON THE GROUP TYPE: " + groupType.name.toUpperCase() + " - See group type table - " + businessRule + " ***ADDED***</font><br/><br/>");
}
}
}
}
//if any groups are removed run this
if(!removed.nil()){
//get the groups in an array and split them to individual sys_id's
var removedArray = removed.split(",");
//cycle through the sys_id's
for (var i=0; i< removedArray.length; i++) {
//get the group type record by sys_id
var groupType1 = new GlideRecord("sys_user_group_type");
groupType1.get(removedArray[i]);
//See if there is a role and if it should auto apply.
var auto1 = groupType1.u_auto;
var grRole1 = groupType1.u_roles;
//if there is a role, run this
if(grRole1 != ''){
//check to see if the group has the role, If so, delete the role
if (auto1 == true){
var multiRoles1 = grRole1.split(",");
for (var j=0; j< multiRoles1.length; j++) {
var GroupHasRole1 = new GlideRecord("sys_group_has_role");
GroupHasRole1.addQuery('group', current.sys_id);
GroupHasRole1.addQuery('role', multiRoles1[j]);
GroupHasRole1.addQuery('inherits', 'true');
GroupHasRole1.query();
if (GroupHasRole1.next()){
GroupHasRole1.deleteRecord();
}
}
//Add alert to notify of group role
gs.addErrorMessage(font + "***REMOVED*** " + grRole1.getDisplayValue().toUpperCase() + " - ROLE HAS BEEN REMOVED FROM THE GROUP BASED ON THE GROUP TYPE: " + groupType1.name.toUpperCase() + " - See group type table - " + businessRule + " ***REMOVED***</font><br/><br/>");
}
}
}
}
})(current, previous);