Last active
December 25, 2015 09:29
-
-
Save cmcdevitt/6954780 to your computer and use it in GitHub Desktop.
ServiceNow -- Go through all your groups and add the ITIL role to each group.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Set the ITIL Role on all Groups in ServiceNow (Calgary) | |
//Run this as a Scheduled Job | |
cm_SetItilRoleToGroups(); | |
function cm_SetItilRoleToGroups(){ | |
//**** --> Change this variable to reflect the Role you want to set on All Groups <-- **** | |
var roleNameForAllGroups = 'itil'; | |
var groupCount = 0; ///How many groups | |
var addCount = 0; //How many times did I add the role to a group | |
var skipCount = 0; //How many times did I skip adding the role to a group | |
//Find the sys_id for the Role we want to set on all Groups | |
//The sys_id is required to set on the 'role' field on sys_group_has_role | |
var rol = new GlideRecord('sys_user_role'); | |
rol.get(roleNameForAllGroups); | |
var roleSysId = rol.sys_id; | |
//Get a list of 'Active' Groups | |
var groups = new GlideRecord('sys_user_group'); | |
groups.addActiveQuery(); //If it's active... | |
groups.query(); | |
//For testing only! | |
//var retRows = groups.getRowCount(); | |
//gs.log("Groups: " + retRows, 'Chris'); | |
//This table holds the Group to role relationship (Many-to-Many) | |
//Use this object to update table | |
var grpHasRole = new GlideRecord('sys_group_has_role'); | |
//Loop through all Groups and Query 'sys_group_has_role' to see if the role is set | |
while(groups.next()){ | |
//Check to see if Group we are working with has the Role | |
var currentGroup = groups.sys_id;//Get the sys_id to set the 'group' field | |
var findGroup = new GlideRecord('sys_group_has_role'); | |
findGroup.addQuery('group', currentGroup); | |
findGroup.addQuery('role', '=', roleSysId); | |
findGroup.query(); | |
//For testing only! | |
//var retRows2 = findGroup.getRowCount(); | |
//gs.log("~~~GHR: " + retRows2 + "GRP: " + currentGroup, 'Chris'); | |
//If the query return nothing, then add the role to the group we are working with | |
if(!findGroup.next()){ | |
//gs.log("Adding " + roleNameForAllGroups + " to " + groups.name, 'Chris'); | |
//Add Role to Group becouse it does not have it yet | |
grpHasRole.initialize(); | |
grpHasRole.group = currentGroup; | |
grpHasRole.role = roleSysId; | |
grpHasRole.insert(); | |
addCount++; | |
}else{ | |
//gs.log("Skiping group " + groups.name + " becouse it has role " + roleNameForAllGroups, 'Chris'); | |
skipCount++; | |
} | |
//Destroy object to ensure fresh search... seems to need this... | |
findGroup = null; | |
groupCount++; | |
} | |
gs.log("Added Role " + roleNameForAllGroups | |
+ " to " + addCount | |
+ " Groups and skipped " + skipCount | |
+ " out of " + groupCount + " Groups", 'Chris'); | |
}// End Function | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment