/**
* @author Andrei Karanko
* @date 8/25/2023
* description
*/
global with sharing class SendEmailInvocable {
@InvocableMethod(Label='Send Email Invocable')
global static void sendEmail(List<SendEmailRequest> requests){
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(SendEmailRequest request : requests){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Messaging.SingleEmailMessage tempMail = Messaging.renderStoredEmailTemplate(
request.templateId,
null,
request.recordId
);
mail.setHtmlBody(tempMail.getHtmlBody());
mail.setSubject(tempMail.getSubject());
mail.setSenderDisplayName(tempMail.getSenderDisplayName());
mail.setReplyTo(request.fromAddress);
mail.setToAddresses(request.toAddresses);
mail.setCcAddresses(request.ccAddresses);
mail.setWhatId(request.recordId);
if(request.fromAddress != null){
List<OrgWideEmailAddress> addresses = [SELECT Id FROM OrgWideEmailAddress WHERE Address =: request.fromAddress];
if(addresses.size() > 0){
mail.setOrgWideEmailAddress(addresses[0].Id);
}
}
emails.add(mail);
}
if( !Test.isRunningTest() && !emails.isEmpty()){
EmailDelegationService.sendMessages(emails);
}
}
global class SendEmailRequest{
@InvocableVariable
global String recordId;
@InvocableVariable
global String templateId;
@InvocableVariable
global List<String> toAddresses;
@InvocableVariable
global String fromAddress;
@InvocableVariable
global List<String> ccAddresses;
}
}