Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. trigger EmailToTask on EmailMessage (after insert) {
  2. list<Task> lstTask = new list<Task>();
  3. map<String,Id> mapEmail = new map<String,Id>();
  4. map<String,Id> mapWorkEmail = new map<String,Id>();
  5. map<String,Id> mapHomeEmail = new map<String,Id>();
  6.  
  7. for(Contact con: [SELECT Id,Email,npe01__WorkEmail__c,npe01__HomeEmail__c FROM Contact]){
  8. if(string.isNotBlank(con.Email))
  9. mapEmail.put(con.Email,con.Id);
  10. if(string.isNotBlank(con.npe01__WorkEmail__c))
  11. mapWorkEmail.put(con.npe01__WorkEmail__c,con.Id);
  12. if(string.isNotBlank(con.npe01__HomeEmail__c))
  13. mapHomeEmail.put(con.npe01__HomeEmail__c,con.Id);
  14. }
  15.  
  16. for (EmailMessage e : Trigger.new) {
  17. if (e.RelatedToId != NULL
  18. && (mapEmail <> NULL && !mapEmail.isEmpty()
  19. || mapWorkEmail <> NULL && !mapWorkEmail.isEmpty()
  20. || mapHomeEmail <> NULL && !mapHomeEmail.isEmpty())
  21. ) {
  22. Task t = new Task();
  23. t.ActivityDate = date.today();
  24. t.Description = e.TextBody;
  25. t.Move_Type__c = 'Personal Note/Email from Donor';
  26. t.Priority = 'Normal';
  27. t.Status = 'Completed';
  28. t.Subject = e.Subject;
  29. t.WhatId = e.RelatedToId;
  30. if(mapEmail.containsKey(e.ToAddress)){
  31. t.WhoId = mapEmail.get(e.ToAddress);
  32. }
  33. else if(mapWorkEmail.containsKey(e.ToAddress )){
  34. t.WhoId = mapWorkEmail.get(e.ToAddress);
  35. }
  36. else if(mapHomeEmail.containsKey(e.ToAddress )){
  37. t.WhoId = mapHomeEmail.get(e.ToAddress);
  38. }
  39. lstTask.add(t);
  40. }
  41. }
  42. if(lstTask <> NULL && !lstTask.isEmpty()){
  43. insert lstTask;
  44. }}
  45.  
  46. @isTest
  47. public with sharing class TestEmailTrigger {
  48.  
  49. @isTest static void test_method_c() {
  50. Account a = new Account();
  51. a.Name = 'Bob Smith Household';
  52. insert a;
  53.  
  54. Contact c = new Contact();
  55. c.lastname = 'Smith';
  56. c.FirstName = 'Bob';
  57. c.AccountId = a.Id;
  58. c.npe01__HomeEmail__c = 'bob.test@gmail.com';
  59. c.npe01__WorkEmail__c = 'bob.test+5@gmail.com';
  60. c.Email = NULL;
  61. c.npe01__Preferred_Email__c = 'Work';
  62. insert c;
  63.  
  64. EmailMessage e = new EmailMessage();
  65. e.BccAddress = 'reporting@foodcorps.org';
  66. e.FromAddress = 'reporting@foodcorps.org';
  67. e.FromName = 'Medhanie Habte';
  68. e.MessageDate = datetime.now();
  69. e.MessageIdentifier = '';
  70. e.Status = '3';
  71. e.Subject = 'test';
  72. e.TextBody = 'test';
  73. e.RelatedToId = c.AccountId;
  74. e.ToAddress = 'bob.test@gmail.com';
  75. e.ValidatedFromAddress = 'reporting@foodcorps.org';
  76. insert e;
  77.  
  78. Task t = new Task();
  79. t.ActivityDate = date.today();
  80. t.Description = e.TextBody;
  81. t.Move_Type__c = 'Personal Note/Email from Donor';
  82. t.Priority = 'Normal';
  83. t.Status = 'Completed';
  84. t.Subject = e.Subject;
  85. t.WhatId = e.RelatedToId;
  86. t.WhoId = c.Id;
  87. insert t;
  88. }
  89.  
  90. @isTest static void test_method_b() {
  91. Account a = new Account();
  92. a.Name = 'Bob Smith Household';
  93. insert a;
  94.  
  95. Contact c = new Contact();
  96. c.lastname = 'Smith';
  97. c.FirstName = 'Bob';
  98. c.AccountId = a.Id;
  99. c.npe01__HomeEmail__c = '';
  100. c.npe01__WorkEmail__c = 'bob.test+5@gmail.com';
  101. c.Email = NULL;
  102. c.npe01__Preferred_Email__c = 'Work';
  103. insert c;
  104.  
  105. EmailMessage e = new EmailMessage();
  106. e.BccAddress = 'reporting@foodcorps.org';
  107. e.FromAddress = 'reporting@foodcorps.org';
  108. e.FromName = 'Medhanie Habte';
  109. e.MessageDate = datetime.now();
  110. e.MessageIdentifier = '';
  111. e.Status = '3';
  112. e.Subject = 'test';
  113. e.TextBody = 'test';
  114. e.RelatedToId = c.AccountId;
  115. e.ToAddress = 'bob.test+5@gmail.com';
  116. e.ValidatedFromAddress = 'reporting@foodcorps.org';
  117. insert e;
  118.  
  119. Task t = new Task();
  120. t.ActivityDate = date.today();
  121. t.Description = e.TextBody;
  122. t.Move_Type__c = 'Personal Note/Email from Donor';
  123. t.Priority = 'Normal';
  124. t.Status = 'Completed';
  125. t.Subject = e.Subject;
  126. t.WhatId = e.RelatedToId;
  127. t.WhoId = c.Id;
  128. insert t;
  129. }
  130. @isTest static void test_method_d() {
  131. Account a = new Account();
  132. a.Name = 'Bob Smith Household';
  133. insert a;
  134.  
  135. Contact c = new Contact();
  136. c.lastname = 'Smith';
  137. c.FirstName = 'Bob';
  138. c.AccountId = a.Id;
  139. c.npe01__HomeEmail__c = 'bob.test+5@gmail.com';
  140. c.npe01__WorkEmail__c = NULL;
  141. c.Email = NULL;
  142. c.npe01__Preferred_Email__c = 'Work';
  143. insert c;
  144.  
  145. EmailMessage e = new EmailMessage();
  146. e.BccAddress = 'reporting@foodcorps.org';
  147. e.FromAddress = 'reporting@foodcorps.org';
  148. e.FromName = 'Medhanie Habte';
  149. e.MessageDate = datetime.now();
  150. e.MessageIdentifier = '';
  151. e.Status = '3';
  152. e.Subject = 'test';
  153. e.TextBody = 'test';
  154. e.RelatedToId = c.AccountId;
  155. e.ToAddress = 'bob.test+5@gmail.com';
  156. e.ValidatedFromAddress = 'reporting@foodcorps.org';
  157. insert e;
  158.  
  159. Task t = new Task();
  160. t.ActivityDate = date.today();
  161. t.Description = e.TextBody;
  162. t.Move_Type__c = 'Personal Note/Email from Donor';
  163. t.Priority = 'Normal';
  164. t.Status = 'Completed';
  165. t.Subject = e.Subject;
  166. t.WhatId = e.RelatedToId;
  167. t.WhoId = c.Id;
  168. insert t;} @isTest static void test_method_e() {
  169. Account a = new Account();
  170. a.Name = 'Bob Smith Household';
  171. insert a;
  172.  
  173. Contact c = new Contact();
  174. c.lastname = 'Smith';
  175. c.FirstName = 'Bob';
  176. c.AccountId = a.Id;
  177. c.npe01__HomeEmail__c = 'bob.test@gmail.com';
  178. c.npe01__WorkEmail__c = 'bob.test+5@gmail.com';
  179. c.Email = 'bob.test@aoltv.com';
  180. c.npe01__Preferred_Email__c = 'Work';
  181. insert c;
  182.  
  183. EmailMessage e = new EmailMessage();
  184. e.BccAddress = 'reporting@foodcorps.org';
  185. e.FromAddress = 'reporting@foodcorps.org';
  186. e.FromName = 'Medhanie Habte';
  187. e.MessageDate = datetime.now();
  188. e.MessageIdentifier = '';
  189. e.Status = '3';
  190. e.Subject = 'test';
  191. e.TextBody = 'test';
  192. e.RelatedToId = c.AccountId;
  193. e.ToAddress = 'bob.test@gmail.com';
  194. e.ValidatedFromAddress = 'reporting@foodcorps.org';
  195. insert e;
  196.  
  197. Task t = new Task();
  198. t.ActivityDate = date.today();
  199. t.Description = e.TextBody;
  200. t.Move_Type__c = 'Personal Note/Email from Donor';
  201. t.Priority = 'Normal';
  202. t.Status = 'Completed';
  203. t.Subject = e.Subject;
  204. t.WhatId = e.RelatedToId;
  205. t.WhoId = c.Id;
  206. insert t;}}
  207.  
  208. || mapWorkEmail <> NULL && !mapWorkEmail.isEmpty()
  209. || mapHomeEmail <> NULL && !mapHomeEmail.isEmpty())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement