Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // bg_OpportunityUtils
- //
- // Utils class for the Opportunity object
- //
- // @Author: Joshua Belton - BrightGen Ltd
- // @Created: 22/02/2019
- // @Changes:
- // 2019-02-28 DW : Case 00041067 Changes to map creation.
- ////////////////////////////////////////////////////////////////////////////////
- public class bg_OpportunityUtils
- {
- //Set up email method
- @TestVisible
- private static Messaging.SendEmailError[] emailErrorsResult;
- @TestVisible
- private static list<Messaging.SingleEmailMessage> mailList = new list<Messaging.SingleEmailMessage>();
- @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.')
- public static List<opportunityEmailParams> sendOpportunityEmailToRelatedContacts(List<opportunityEmailParams> getParams)
- {
- System.debug('--- Running sendOpportunityEmailToRelatedContacts ---');
- opportunityEmailParams params = getParams[0];
- if(getParams.size() > 1 )
- {
- throw new bg_extraParamsException('Cannot have more than one Opportunity.');
- }
- // Get all of the templates and put into a list
- 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)]);
- // Set Ids of template names referenced in Invoked Params
- String emailTemplateAId;
- String emailTemplateBId;
- String emailTemplateCId;
- for(EmailTemplate template : mapOfEmailTemplates.values())
- {
- if(template.Name == params.emailTemplateA)
- {
- emailTemplateAId = template.Id;
- }
- if(template.Name == params.emailTemplateB)
- {
- emailTemplateBId = template.Id;
- }
- if(template.Name == params.emailTemplateC)
- {
- emailTemplateCId = template.Id;
- }
- }
- if(emailTemplateAId == null)
- {
- throw new bg_EmailTemplateException('The "'+params.emailTemplateA+'" template could not be found.');
- }
- if(emailTemplateBId == null)
- {
- throw new bg_EmailTemplateException('The "'+params.emailTemplateB+'" template could not be found.');
- }
- if(emailTemplateCId == null)
- {
- throw new bg_EmailTemplateException('The "'+params.emailTemplateC+'" template could not be found.');
- }
- //Get the email from the related Account on the Opp
- List<Opportunity> opportunities = new List<Opportunity>([SELECT Id, AccountId, Account.PersonEmail, Account.PersonContactId,
- Account.Email_2__c, Account.Email_3__c,
- Account.Email_4__c, Additional_Buyer__r.PersonEmail, Additional_Buyer__r.PersonContactId,
- Additional_Buyer__r.Email_2__c, Additional_Buyer__r.Email_3__c,
- Additional_Buyer__r.Email_4__c, Additional_Buyer_2__r.PersonEmail, Additional_Buyer_2__r.PersonContactId,
- Additional_Buyer_2__r.Email_2__c,Additional_Buyer_2__r.Email_3__c,
- Additional_Buyer_2__r.Email_4__c, Additional_Buyer_3__r.PersonEmail, Additional_Buyer_3__r.PersonContactId,
- Additional_Buyer_3__r.Email_2__c, Additional_Buyer_3__r.Email_3__c,
- Additional_Buyer_3__r.Email_4__c
- FROM Opportunity
- WHERE Id = :params.opportunityId]);
- if(opportunities.size() == 0 || opportunities.size() > 2)
- {
- throw new bg_opportunityException('Must have one Opportunity to gather emails.');
- }
- Opportunity oppByParamsId = opportunities[0];//new Opportunity();
- //oppByParamsId.Id = opportunities[0].Id;
- //Check to see if the Primary buyer additional emails are empty, if not add to respecitve list
- List<String> primBuyerAddEmails = new List<String>();
- if(oppByParamsId.Account.Email_2__c != null)
- {
- primBuyerAddEmails.add(oppByParamsId.Account.Email_2__c);
- }
- if(oppByParamsId.Account.Email_3__c != null)
- {
- primBuyerAddEmails.add(opportunities.get(0).Account.Email_3__c);
- }
- if(opportunities.get(0).Account.Email_4__c != null)
- {
- primBuyerAddEmails.add(oppByParamsId.Account.Email_4__c);
- }
- //Check to see if the 1st additional buyer additional emails are empty, if not add to respective list
- List<String> additionalBuyerAddEmails = new List<String>();
- if(opportunities.get(0).Additional_Buyer__r.Email_2__c != null)
- {
- additionalBuyerAddEmails.add(opportunities.get(0).Additional_Buyer__r.Email_2__c);
- }
- if(opportunities.get(0).Additional_Buyer__r.Email_3__c != null)
- {
- additionalBuyerAddEmails.add(opportunities.get(0).Additional_Buyer__r.Email_3__c);
- }
- if(opportunities.get(0).Additional_Buyer__r.Email_4__c != null)
- {
- additionalBuyerAddEmails.add(opportunities.get(0).Additional_Buyer__r.Email_4__c);
- }
- //Check to see if the 2nd additional buyer additional emails are empty, if not add to respective list
- List<String> additionalBuyerAddEmails2 = new List<String>();
- if(opportunities.get(0).Additional_Buyer_2__r.Email_2__c != null)
- {
- additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_2__c);
- }
- if(opportunities.get(0).Additional_Buyer_2__r.Email_3__c != null)
- {
- additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_3__c);
- }
- if(opportunities.get(0).Additional_Buyer_2__r.Email_4__c != null)
- {
- additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_4__c);
- }
- //Check to see if the 3rd additional buyer additional emails are empty, if not add to respective list
- List<String> additionalBuyerAddEmails3 = new List<String>();
- if(opportunities.get(0).Additional_Buyer_3__r.Email_2__c != null)
- {
- additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_2__c);
- }
- if(opportunities.get(0).Additional_Buyer_3__r.Email_3__c != null)
- {
- additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_3__c);
- }
- if(opportunities.get(0).Additional_Buyer_3__r.Email_4__c != null)
- {
- additionalBuyerAddEmails2.add(opportunities.get(0).Additional_Buyer_2__r.Email_4__c);
- }
- //Create list for all of the additional emails on each person account
- List<String> allAdditionalEmails = new List<String>();
- //If the list is not null then add the list to a new list
- if(primBuyerAddEmails != null)
- {
- allAdditionalEmails.addAll(primBuyerAddEmails);
- }
- if(additionalBuyerAddEmails != null)
- {
- allAdditionalEmails.addAll(additionalBuyerAddEmails);
- }
- if(additionalBuyerAddEmails2 != null)
- {
- allAdditionalEmails.addAll(additionalBuyerAddEmails2);
- }
- if(additionalBuyerAddEmails3 != null)
- {
- allAdditionalEmails.addAll(additionalBuyerAddEmails3);
- }
- Account primaryBuyer = opportunities.get(0).Account;
- Account additionalBuyer = opportunities.get(0).Additional_Buyer__r;
- Account additionalBuyer2 = opportunities.get(0).Additional_Buyer_2__r;
- Account additionalBuyer3 = opportunities.get(0).Additional_Buyer_3__r;
- list<Account> accList = new list<Account>();
- if (!opportunities.isEmpty())
- {
- Opportunity opportunity = opportunities.get(0);
- // Primary buyer email addresses
- if(primaryBuyer != null)
- {
- accList.add(primaryBuyer);
- }
- // Additional buyer 1 email addresses
- if(additionalBuyer != null)
- {
- accList.add(additionalBuyer);
- }
- // Additional buyer 2 email addresses
- if(additionalBuyer2 != null)
- {
- accList.add(additionalBuyer2);
- }
- // Additional buyer 3 email addresses
- if(additionalBuyer3 != null)
- {
- accList.add(additionalBuyer3);
- }
- }
- //Get all of the completed assignees that are related to the current opp
- 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,
- Assignee_Name__r.PersonContactId, Assignee_2__r.PersonContactId, Assignee_3__r.PersonContactId, Assignee_4__r.PersonContactId
- FROM Further_Assignments__c WHERE Opportunity__c = :oppByParamsId.Id AND Type__c = 'Complete']);
- //Boolean sendToBuyersOnly = false;
- //Boolean sendToBoth = false;
- // Get all Accounts from the Further_Assignments__c (assingees) - needed to provide list of Accounts to act as targetObjectId for email
- Set<Id> assigneeAccountsIds = new Set<Id>();
- // Is there any completed Further_Assignments__c ?
- if(furtherAssignmentById.size() > 0)
- {
- //sendToBoth = true;
- // Send template B to the Buyers and template C to the Assignees
- for(Further_Assignments__c fa : furtherAssignmentById.values())
- {
- assigneeAccountsIds.add(fa.Assignee_Name__r.Id);
- assigneeAccountsIds.add(fa.Assignee_2__r.Id);
- assigneeAccountsIds.add(fa.Assignee_3__r.Id);
- assigneeAccountsIds.add(fa.Assignee_4__r.Id);
- }
- }
- //else
- //{
- // Send template A to the Buyers only
- //sendToBuyersOnly = true;
- //}
- list<Account> assigneeAccounts = [SELECT Id, PersonEmail, Name FROM Account WHERE Id IN :assigneeAccountsIds AND PersonEmail != null];
- Id buyerTemplate;
- Id assingeeTemplate;
- // Set templates, sending to only the buyer unless we found assignees
- if(assigneeAccounts.size() == 0)
- {
- buyerTemplate = emailTemplateAId;
- }
- else
- {
- buyerTemplate = emailTemplateBId;
- assingeeTemplate = emailTemplateCId;
- }
- // Send emails
- if(accList.size() > 0)
- {
- for(Account acc : accList){
- Messaging.SingleEmailMessage mailBuyer = new Messaging.SingleEmailMessage();
- mailBuyer.setTemplateId(buyerTemplate);
- mailBuyer.setCcAddresses(allAdditionalEmails);
- mailBuyer.setTargetObjectId(acc.Id);
- mailBuyer.setWhatId(params.opportunityId);
- mailList.add(mailBuyer);
- }
- }
- if(assigneeAccounts.size() > 0)
- {
- for(Account acc : assigneeAccounts){
- Messaging.SingleEmailMessage mailAssignees = new Messaging.SingleEmailMessage();
- mailAssignees.setTemplateId(assingeeTemplate);
- mailAssignees.setTargetObjectId(acc.Id);
- mailAssignees.setWhatId(params.opportunityId);
- mailList.add(mailAssignees);
- }
- }
- if(mailList.size()>0)
- {
- emailErrorsResult = sendEmails(mailList);
- }
- return null;
- }
- public class opportunityEmailParams
- {
- @InvocableVariable(required=true)
- public String emailTemplateA;
- @InvocableVariable(required=true)
- public String emailTemplateB;
- @InvocableVariable(required=true)
- public String emailTemplateC;
- @InvocableVariable(required=true)
- public Id opportunityId;
- }
- public static Messaging.SendEmailError[] sendEmails(List<Messaging.SingleEmailMessage> emails)
- {
- Messaging.SendEmailResult[] sendResults;
- if (!Test.isRunningTest())
- {
- sendResults = Messaging.sendEmail(emails);
- }
- else
- {
- sendResults = new Messaging.SendEmailResult[]{}; // Test Context, prevents NO_MASS_MAIL_PERMISSION errors etc
- }
- Messaging.SendEmailError[] errors = new Messaging.SendEmailError[]{};
- for (Messaging.SendEmailResult res : sendResults)
- {
- errors.addAll(res.getErrors());
- }
- return errors;
- }
- public class bg_EmailTemplateException extends Exception {
- }
- public class bg_opportunityException extends Exception {
- }
- public class bg_extraParamsException extends Exception {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement