Background: I want to add all users who are assigned a specific permission set to a specific chatter group. I have a simple version of this working, but would like to abstract this to use a list custom setting to store ChatterGroupName and PermissionSetName so I can run this on N pairings, and then make it scheduleable and perhaps batchable tho I dont think the data volume will need to go batchable.
Question: I cant wrap my head around a pattern to use in moving from my little proof of concept that has one method to handle one permission set / chatter group, to one that works w N number of records from a custom setting.
Where I'm at: I created a simple class and called it via execute anonymous and amazingly enough users were added to the chatter group as desired. I have created a custom setting and added 2 records in it, and i added a line to my controller to populate a list with the rows from it. And...now I dont really know what to do next, besides for coming up with a more concise name for the custom setting.
I feel like I could wrap the whole thing in a giant for
loop, but that just seems wrong. My sense is that I need to use maps, but I dont really have the experience w/ maps needed to think through how to proceed, so requesting some top-level help on how best to do so. Also would appreciate any tips / flags for things to look out for that might blow up now or when I try to go scheduleable.
Controller Code:
public class UserAddToChatterGroupFromPermSet {
list <Permission_Set_Chatter_Group_Map__c> pscg = new list <Permission_Set_Chatter_Group_Map__c> ([SELECT Permission_Set_Name__c, Chatter_Group_Name__c FROM Permission_Set_Chatter_Group_Map__c WHERE Active__c = TRUE]);
list <user> psusers = new list<user>();
list <permissionsetassignment> psa = new list <permissionsetassignment>();
permissionset permset = new permissionset();
collaborationgroup cg = new collaborationgroup();
set <Id> permSetUserIds = new set<Id>();
set <Id> cgUserIds = new set<Id>();
public UserAddToChatterGroupFromPermSet(){
addUsersToEchosignGroup();
}
public void addUsersToEchosignGroup() {
//get all active users currently assigned permission set
permset = [SELECT Id from PermissionSet WHERE Label = 'App: Echosign' LIMIT 1];
system.debug('permset id = ' + permset);
psusers = [SELECT Name, ID from USER WHERE ID IN (SELECT AssigneeID from PermissionSetAssignment WHERE PermissionSetId =: permset.Id) AND IsActive = TRUE];
system.debug('users = ' + psusers);
//get all group members currently in chatter group
cg = [SELECT ID from CollaborationGroup WHERE Name = 'E-Signature Users (Adobe Echosign)'];
list <CollaborationGroupMember> cgusers = new list <CollaborationGroupMember>([SELECT MemberId from CollaborationGroupMember WHERE CollaborationGroupId =: cg.Id ]);
system.debug('cgusers = ' + cgusers);
//compare existing perm set users ID to collaboration group members ID and prepare list of cgmembers to insert
FOR(CollaborationGroupMember cguser : cgusers){
cguserIds.add(cguser.MemberId);
}
list <CollaborationGroupMember> cgUsersToInsert = new list <CollaborationGroupMember>();
FOR(User psuser : psusers){
CollaborationGroupMember cgmember = new CollaborationGroupMember();
IF(cguserIds.contains(psuser.Id)){
}
ELSE{
cgmember.MemberId = psuser.Id;
cgmember.CollaborationGroupId = cg.Id;
cgusersToInsert.add(cgmember);
}
}
system.debug('cgUsersToInsertsize = ' + cgusersToInsert.size());
IF(cgUsersToInsert.size() >= 1){
insert(cgUsersToInsert);
}
ELSE{
system.debug('no cg members to insert');
}
}
}