fork download
  1. /**
  2. * @author Andrei Karanko
  3. * @date 8/25/2023
  4. * description
  5. */
  6. global with sharing class SendEmailInvocable {
  7. @InvocableMethod(Label='Send Email Invocable')
  8. global static void sendEmail(List<SendEmailRequest> requests){
  9. List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
  10. for(SendEmailRequest request : requests){
  11. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  12. Messaging.SingleEmailMessage tempMail = Messaging.renderStoredEmailTemplate(
  13. request.templateId,
  14. null,
  15. request.recordId
  16. );
  17. mail.setHtmlBody(tempMail.getHtmlBody());
  18. mail.setSubject(tempMail.getSubject());
  19. mail.setSenderDisplayName(tempMail.getSenderDisplayName());
  20. mail.setReplyTo(request.fromAddress);
  21. mail.setToAddresses(request.toAddresses);
  22. mail.setCcAddresses(request.ccAddresses);
  23. mail.setWhatId(request.recordId);
  24. if(request.fromAddress != null){
  25. List<OrgWideEmailAddress> addresses = [SELECT Id FROM OrgWideEmailAddress WHERE Address =: request.fromAddress];
  26. if(addresses.size() > 0){
  27. mail.setOrgWideEmailAddress(addresses[0].Id);
  28. }
  29. }
  30. emails.add(mail);
  31. }
  32. if( !Test.isRunningTest() && !emails.isEmpty()){
  33. EmailDelegationService.sendMessages(emails);
  34. }
  35. }
  36.  
  37. global class SendEmailRequest{
  38. @InvocableVariable
  39. global String recordId;
  40. @InvocableVariable
  41. global String templateId;
  42. @InvocableVariable
  43. global List<String> toAddresses;
  44. @InvocableVariable
  45. global String fromAddress;
  46. @InvocableVariable
  47. global List<String> ccAddresses;
  48.  
  49. }
  50. }
  51.  
Success #stdin #stdout #stderr 0.02s 11068KB
stdin
Standard input is empty
stdout
Object: nil error: did not understand #associationAt:ifAbsent:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #associationAt:ifAbsent: (SysExcept.st:1448)
DeferredVariableBinding>>resolvePathFrom: (DeferBinding.st:115)
DeferredVariableBinding>>value (DeferBinding.st:69)
UndefinedObject>>executeStatements (prog:32)
Object: nil error: did not understand #associationAt:ifAbsent:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #associationAt:ifAbsent: (SysExcept.st:1448)
DeferredVariableBinding>>resolvePathFrom: (DeferBinding.st:115)
DeferredVariableBinding>>value (DeferBinding.st:69)
UndefinedObject>>executeStatements (prog:32)
stderr
./prog:1: expected expression
./prog:24: expected expression
./prog:32: expected expression
./prog:32: expected expression