Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. trigger Addtogroup1 on Contact (after insert, after update) {
  2.  
  3. List<GroupMember> GMlist = new List<GroupMember>();
  4. Set<String> contactEmails = new Set<String>();
  5. for(Contact con : Trigger.New) {
  6. //create a set with the contact email addresses
  7. contactEmails.add(con.email);
  8. }
  9.  
  10. //query for the related users and put them in a map,
  11. //where the key is the email and the value is the user
  12. Map<String, User> emailUserMap = new Map<String, User> ();
  13. for(User aUser : [select id, email from User where email in : contactEmails]){
  14. emailUserMap.put(aUser.email, aUser);
  15. }
  16. system.debug(emailUserMap);
  17. List<Id> userIdList = new List<Id>();
  18. for(Contact con : Trigger.New) {
  19. if(con.Public_Group_Service_Partner_Content1__c == TRUE) {
  20.  
  21. userIdList.add(emailUserMap.get(con.email).id);
  22.  
  23. }
  24. }
  25.  
  26. //dymanically get the get group id.
  27. Group theGroup = [select id from Group where Name = 'Service Partner Content'];
  28. if(null != theGroup){
  29. //call the contact trigger helper if the group exists.
  30. //This method adds the user to the group
  31. ContactTriggerHelper.addUsersToGroup(theGroup.id,userIdList );
  32. }
  33.  
  34. public class ContactTriggerHelper{
  35.  
  36. //future call to do the group adding. the future call will spawn a new thread.
  37. @future
  38. public static void addUsersToGroup(String groupId, List<Id> userIds){
  39. List<GroupMember> GMlist = new List<GroupMember>();
  40. for(ID userId: userIds){
  41. GroupMember gm = new GroupMember();
  42. gm.GroupId = groupId;
  43. gm.UserOrGroupId = userId;
  44. gmList.add(GM);
  45. }
  46.  
  47.  
  48. if(gmList.size() > 0){
  49. insert gmList;
  50. }
  51. }
  52.  
  53. @isTest
  54.  
  55. // Insert contact with your required field.
  56.  
  57. Contact cnt = new Contact();
  58. cnt.LastName = 'Test contact';
  59. cnt.Email = 'test@test.com';
  60. cnt.Public_Group_Technology_Partner_Content__c = true;
  61. insert cnt;
  62.  
  63. // Insert user
  64.  
  65. Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
  66. User u = new User(Alias = 'standt', Email='test@test.com',
  67. EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
  68. LocaleSidKey='en_US', ProfileId = p.Id,
  69. TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
  70. insert u;
  71.  
  72. // Insert Group
  73. if(isGroup == true)
  74. {
  75. Group grp = new Group();
  76. grp.Name = 'Technology Partner Content';
  77. grp.DeveloperName = 'Technology Partner Content';
  78. insert grp;
  79. }
  80. }
  81.  
  82.  
  83.  
  84. static testMethod void insertWithGroup()
  85. {
  86. createGroup(true);
  87. }
  88.  
  89. static testMethod void insertWithoutGroup()
  90. {
  91. createGroup(false);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement