Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.91 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // bg_OpportunityUtils
  3. //
  4. // Utils class for the Opportunity object
  5. //
  6. // @Author: Joshua Belton - BrightGen Ltd
  7. // @Created: 22/02/2019
  8. // @Changes:
  9. // 2019-02-28 DW : Case 00041067 Changes to map creation.
  10. ////////////////////////////////////////////////////////////////////////////////
  11. public class bg_OpportunityUtils
  12. {
  13.  
  14. //Set up email method
  15. @TestVisible
  16. private static Messaging.SendEmailError[] emailErrorsResult;
  17. @TestVisible
  18. private static list<Messaging.SingleEmailMessage> mailList = new list<Messaging.SingleEmailMessage>();
  19.  
  20. @InvocableMethod(label='Send Opportunity emails to related contacts' description='Method to get the appropriate buyers and assignees and send an email based on the templates provided.')
  21. public static List<opportunityEmailParams> sendOpportunityEmailToRelatedContacts(List<opportunityEmailParams> getParams)
  22. {
  23. System.debug('--- Running sendOpportunityEmailToRelatedContacts ---');
  24.  
  25. opportunityEmailParams params = getParams[0];
  26.  
  27. if(getParams.size() > 1 )
  28. {
  29. throw new bg_extraParamsException('Cannot have more than one Opportunity.');
  30. }
  31.  
  32. // Get all of the templates and put into a list
  33. Map<Id, EmailTemplate> mapOfEmailTemplates = new Map<Id, EmailTemplate>([SELECT Id, Name FROM EmailTemplate WHERE (Name = :params.emailTemplateA OR Name = :params.emailTemplateB OR Name = :params.emailTemplateC)]);
  34.  
  35. // Set Ids of template names referenced in Invoked Params
  36. String emailTemplateAId;
  37. String emailTemplateBId;
  38. String emailTemplateCId;
  39.  
  40. for(EmailTemplate template : mapOfEmailTemplates.values())
  41. {
  42. if(template.Name == params.emailTemplateA)
  43. {
  44. emailTemplateAId = template.Id;
  45. }
  46. if(template.Name == params.emailTemplateB)
  47. {
  48. emailTemplateBId = template.Id;
  49. }
  50. if(template.Name == params.emailTemplateC)
  51. {
  52. emailTemplateCId = template.Id;
  53. }
  54. }
  55.  
  56. if(emailTemplateAId == null)
  57. {
  58. throw new bg_EmailTemplateException('The "'+params.emailTemplateA+'" template could not be found.');
  59. }
  60. if(emailTemplateBId == null)
  61. {
  62. throw new bg_EmailTemplateException('The "'+params.emailTemplateB+'" template could not be found.');
  63. }
  64. if(emailTemplateCId == null)
  65. {
  66. throw new bg_EmailTemplateException('The "'+params.emailTemplateC+'" template could not be found.');
  67. }
  68.  
  69. //Get the email from the related Account on the Opp
  70. List<Opportunity> opportunities = new List<Opportunity>([SELECT Id, AccountId, Account.PersonEmail, Account.PersonContactId,
  71. Account.Email_2__c, Account.Email_3__c,
  72. Account.Email_4__c, Additional_Buyer__r.PersonEmail, Additional_Buyer__r.PersonContactId,
  73. Additional_Buyer__r.Email_2__c, Additional_Buyer__r.Email_3__c,
  74. Additional_Buyer__r.Email_4__c, Additional_Buyer_2__r.PersonEmail, Additional_Buyer_2__r.PersonContactId,
  75. Additional_Buyer_2__r.Email_2__c,Additional_Buyer_2__r.Email_3__c,
  76. Additional_Buyer_2__r.Email_4__c, Additional_Buyer_3__r.PersonEmail, Additional_Buyer_3__r.PersonContactId,
  77. Additional_Buyer_3__r.Email_2__c, Additional_Buyer_3__r.Email_3__c,
  78. Additional_Buyer_3__r.Email_4__c
  79. FROM Opportunity
  80. WHERE Id = :params.opportunityId]);
  81.  
  82.  
  83.  
  84. if(opportunities.size() == 0 || opportunities.size() > 2)
  85. {
  86. throw new bg_opportunityException('Must have one Opportunity to gather emails.');
  87. }
  88.  
  89. Opportunity oppByParamsId = opportunities[0];//new Opportunity();
  90.  
  91. //oppByParamsId.Id = opportunities[0].Id;
  92.  
  93. //Check to see if the Primary buyer additional emails are empty, if not add to respecitve list
  94. List<String> primBuyerAddEmails = new List<String>();
  95. if(oppByParamsId.Account.Email_2__c != null)
  96. {
  97. primBuyerAddEmails.add(oppByParamsId.Account.Email_2__c);
  98. }
  99. if(oppByParamsId.Account.Email_3__c != null)
  100. {
  101. primBuyerAddEmails.add(opportunities.get(0).Account.Email_3__c);
  102. }
  103. if(opportunities.get(0).Account.Email_4__c != null)
  104. {
  105. primBuyerAddEmails.add(oppByParamsId.Account.Email_4__c);
  106. }
  107.  
  108. //Check to see if the 1st additional buyer additional emails are empty, if not add to respective list
  109. List<String> additionalBuyerAddEmails = new List<String>();
  110. if(opportunities.get(0).Additional_Buyer__r.Email_2__c != null)
  111. {
  112. additionalBuyerAddEmails.add(opportunities.get(0).Additional_Buyer__r.Email_2__c);
  113. }
  114. if(opportunities.get(0).Additional_Buyer__r.Email_3__c != null)
  115. {
  116. additionalBuyerAddEmails.add(opportunities.get(0).Additional_Buyer__r.Email_3__c);
  117. }
  118. if(opportunities.get(0).Additional_Buyer__r.Email_4__c != null)
  119. {
  120. additionalBuyerAddEmails.add(opportunities.get(0).Additional_Buyer__r.Email_4__c);
  121. }
  122.  
  123. //Check to see if the 2nd additional buyer additional emails are empty, if not add to respective list
  124. List<String> additionalBuyerAddEmails2 = new List<String>();
  125. if(opportunities.get(0).Additional_Buyer_2__r.Email_2__c != null)
  126. {
  127. additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_2__c);
  128. }
  129. if(opportunities.get(0).Additional_Buyer_2__r.Email_3__c != null)
  130. {
  131. additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_3__c);
  132. }
  133. if(opportunities.get(0).Additional_Buyer_2__r.Email_4__c != null)
  134. {
  135. additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_4__c);
  136. }
  137.  
  138. //Check to see if the 3rd additional buyer additional emails are empty, if not add to respective list
  139. List<String> additionalBuyerAddEmails3 = new List<String>();
  140. if(opportunities.get(0).Additional_Buyer_3__r.Email_2__c != null)
  141. {
  142. additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_2__c);
  143. }
  144. if(opportunities.get(0).Additional_Buyer_3__r.Email_3__c != null)
  145. {
  146. additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_3__c);
  147. }
  148. if(opportunities.get(0).Additional_Buyer_3__r.Email_4__c != null)
  149. {
  150. additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_4__c);
  151. }
  152.  
  153. //Create list for all of the additional emails on each person account
  154. List<String> allAdditionalEmails = new List<String>();
  155.  
  156. //If the list is not null then add the list to a new list
  157. if(primBuyerAddEmails != null)
  158. {
  159. allAdditionalEmails.addAll(primBuyerAddEmails);
  160. }
  161. if(additionalBuyerAddEmails != null)
  162. {
  163. allAdditionalEmails.addAll(additionalBuyerAddEmails);
  164. }
  165. if(additionalBuyerAddEmails2 != null)
  166. {
  167. allAdditionalEmails.addAll(additionalBuyerAddEmails2);
  168. }
  169. if(additionalBuyerAddEmails3 != null)
  170. {
  171. allAdditionalEmails.addAll(additionalBuyerAddEmails3);
  172. }
  173.  
  174.  
  175. Account primaryBuyer = opportunities.get(0).Account;
  176. Account additionalBuyer = opportunities.get(0).Additional_Buyer__r;
  177. Account additionalBuyer2 = opportunities.get(0).Additional_Buyer_2__r;
  178. Account additionalBuyer3 = opportunities.get(0).Additional_Buyer_3__r;
  179.  
  180. list<Account> accList = new list<Account>();
  181.  
  182. if (!opportunities.isEmpty())
  183. {
  184. Opportunity opportunity = opportunities.get(0);
  185.  
  186. // Primary buyer email addresses
  187. if(primaryBuyer != null)
  188. {
  189. accList.add(primaryBuyer);
  190.  
  191. }
  192.  
  193. // Additional buyer 1 email addresses
  194. if(additionalBuyer != null)
  195. {
  196. accList.add(additionalBuyer);
  197.  
  198. }
  199.  
  200. // Additional buyer 2 email addresses
  201. if(additionalBuyer2 != null)
  202. {
  203. accList.add(additionalBuyer2);
  204.  
  205. }
  206.  
  207. // Additional buyer 3 email addresses
  208. if(additionalBuyer3 != null)
  209. {
  210. accList.add(additionalBuyer3);
  211.  
  212. }
  213. }
  214.  
  215. //Get all of the completed assignees that are related to the current opp
  216. Map<Id, Further_Assignments__c> furtherAssignmentById = new Map<Id, Further_Assignments__c>([SELECT Id, Assignee_Name__c, Assignee_2__c, Assignee_3__c, Assignee_4__c,
  217. Assignee_Name__r.PersonContactId, Assignee_2__r.PersonContactId, Assignee_3__r.PersonContactId, Assignee_4__r.PersonContactId
  218. FROM Further_Assignments__c WHERE Opportunity__c = :oppByParamsId.Id AND Type__c = 'Complete']);
  219.  
  220. //Boolean sendToBuyersOnly = false;
  221. //Boolean sendToBoth = false;
  222.  
  223. // Get all Accounts from the Further_Assignments__c (assingees) - needed to provide list of Accounts to act as targetObjectId for email
  224. Set<Id> assigneeAccountsIds = new Set<Id>();
  225.  
  226. // Is there any completed Further_Assignments__c ?
  227. if(furtherAssignmentById.size() > 0)
  228. {
  229. //sendToBoth = true;
  230.  
  231. // Send template B to the Buyers and template C to the Assignees
  232. for(Further_Assignments__c fa : furtherAssignmentById.values())
  233. {
  234. assigneeAccountsIds.add(fa.Assignee_Name__r.Id);
  235. assigneeAccountsIds.add(fa.Assignee_2__r.Id);
  236. assigneeAccountsIds.add(fa.Assignee_3__r.Id);
  237. assigneeAccountsIds.add(fa.Assignee_4__r.Id);
  238. }
  239. }
  240. //else
  241. //{
  242. // Send template A to the Buyers only
  243. //sendToBuyersOnly = true;
  244. //}
  245. list<Account> assigneeAccounts = [SELECT Id, PersonEmail, Name FROM Account WHERE Id IN :assigneeAccountsIds AND PersonEmail != null];
  246.  
  247. Id buyerTemplate;
  248. Id assingeeTemplate;
  249.  
  250. // Set templates, sending to only the buyer unless we found assignees
  251. if(assigneeAccounts.size() == 0)
  252. {
  253. buyerTemplate = emailTemplateAId;
  254. }
  255. else
  256. {
  257. buyerTemplate = emailTemplateBId;
  258. assingeeTemplate = emailTemplateCId;
  259. }
  260.  
  261. // Send emails
  262. if(accList.size() > 0)
  263. {
  264. for(Account acc : accList){
  265. Messaging.SingleEmailMessage mailBuyer = new Messaging.SingleEmailMessage();
  266. mailBuyer.setTemplateId(buyerTemplate);
  267. mailBuyer.setCcAddresses(allAdditionalEmails);
  268. mailBuyer.setTargetObjectId(acc.Id);
  269. mailBuyer.setWhatId(params.opportunityId);
  270. mailList.add(mailBuyer);
  271. }
  272. }
  273. if(assigneeAccounts.size() > 0)
  274. {
  275. for(Account acc : assigneeAccounts){
  276. Messaging.SingleEmailMessage mailAssignees = new Messaging.SingleEmailMessage();
  277. mailAssignees.setTemplateId(assingeeTemplate);
  278. mailAssignees.setTargetObjectId(acc.Id);
  279. mailAssignees.setWhatId(params.opportunityId);
  280. mailList.add(mailAssignees);
  281. }
  282. }
  283.  
  284.  
  285. if(mailList.size()>0)
  286. {
  287. emailErrorsResult = sendEmails(mailList);
  288. }
  289.  
  290. return null;
  291.  
  292. }
  293.  
  294. public class opportunityEmailParams
  295. {
  296. @InvocableVariable(required=true)
  297. public String emailTemplateA;
  298.  
  299. @InvocableVariable(required=true)
  300. public String emailTemplateB;
  301.  
  302. @InvocableVariable(required=true)
  303. public String emailTemplateC;
  304.  
  305. @InvocableVariable(required=true)
  306. public Id opportunityId;
  307. }
  308.  
  309. public static Messaging.SendEmailError[] sendEmails(List<Messaging.SingleEmailMessage> emails)
  310. {
  311. Messaging.SendEmailResult[] sendResults;
  312. if (!Test.isRunningTest())
  313. {
  314. sendResults = Messaging.sendEmail(emails);
  315. }
  316. else
  317. {
  318. sendResults = new Messaging.SendEmailResult[]{}; // Test Context, prevents NO_MASS_MAIL_PERMISSION errors etc
  319. }
  320. Messaging.SendEmailError[] errors = new Messaging.SendEmailError[]{};
  321.  
  322. for (Messaging.SendEmailResult res : sendResults)
  323. {
  324. errors.addAll(res.getErrors());
  325. }
  326. return errors;
  327. }
  328.  
  329. public class bg_EmailTemplateException extends Exception {
  330.  
  331. }
  332.  
  333. public class bg_opportunityException extends Exception {
  334.  
  335. }
  336.  
  337. public class bg_extraParamsException extends Exception {
  338.  
  339. }
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement