Created
December 4, 2013 02:13
-
-
Save cmcdevitt/7781157 to your computer and use it in GitHub Desktop.
Using a Business Rule add a user to a group when a certain notification is added to their device
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
//When a user addes a certain notification to thier device, add them to a group (Group paging) | |
//Business Rule -- ServiceNow | |
//Name:addUserToGroupForNotification | |
//Table: Notification Messages [cmn_notif_message] | |
//Order: 500, When: async, Priority: 500, Insert | |
//If "P1/P2 All Manager Notify New"(sysevent_email_action). | |
//-->Then, add user(sys_user) to "IT Managers" group(sys_user_group)(sys_user_grmember) | |
cm_addUserToGroupForNotification(); | |
function cm_addUserToGroupForNotification(){ | |
//Lookup Group 'IT Managers' | |
var grpItMgr = getSysId('IT Managers', 'sys_user_group'); | |
//Lookup Notification 'P1/P2 All Manager Notify New' | |
var notMessage = getSysId('P1/P2 All Manager Notify New', 'sysevent_email_action'); | |
if(current.notification == notMessage){ | |
addToGroup(current.user, grpItMgr); | |
}else{ | |
//gs.log("Notification does not match", 'Chris'); | |
} | |
function getSysId (itemName,tableName){ | |
//Todo: add varable incase the 'name' field is not correct | |
var gdRec = new GlideRecord(tableName); | |
gdRec.addQuery('name', '=', itemName); | |
gdRec.query(); | |
if(gdRec.next()){ | |
return gdRec.sys_id; | |
}else{ | |
return false; | |
} | |
} | |
function addToGroup(userSysId,groupSysId){ | |
var gdRec = new GlideRecord('sys_user_grmember'); | |
//Check to see if user is already in group | |
gdRec.addQuery('user','=', userSysId); | |
gdRec.addQuery('group','=', groupSysId); | |
gdRec.query(); | |
if(gdRec.next()){ | |
//Found it, so don't insert it | |
//gs.log("User already in group, skipping...", "Chris"); | |
}else{ | |
gdRec.initialize(); | |
gdRec.group = groupSysId; | |
gdRec.user = userSysId; | |
gdRec.insert(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment