0

I'm trying to pass in the flow variable collection of Email addresses to Apex action to send out one email to each email address. I got some idea from Question, the code works fine when i ran it from anonymous window (by passing list of Email addresses) but it doesn't work with the flow. Can someone point me what I'm doing wrong?

Here is the code

public class SendEmailAction {
    @InvocableMethod(label='Send SE Email Notification') 
public static void SendSENotification(List<List<String>> Emailaddresses){
    List<String> getEmails = new List<String>();
    
    //List<string> getEmail = Emailaddresses[0];
    List<OrgWideEmailAddress> lstEmailAddress=[select Id from OrgWideEmailAddress WHERE Address='[email protected]'];
    List<EmailTemplate> SEEmailTemplate= [Select Id, Name, DeveloperName 
                           from EmailTemplate 
                           where DeveloperName= 'Safety_Event_New_Event_Reported'];
         
     Messaging.SingleEmailMessage[] mails = new Messaging.SingleEmailMessage[0];
            
    for(List<String> Ids : Emailaddresses) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateId(SEEmailTemplate[0].Id);
        mail.setTargetObjectId(UserInfo.getUserId());
        mail.setToAddresses(getemails);
        mail.setSaveAsActivity(false);
        mails.add(mail);
        mail.setOrgWideEmailAddressId(lstEmailAddress[0].Id);
    }
   Messaging.sendEmail(mails);
}
}

Flow Flow

LOOP

Assignments

enter image description here

4
  • the code you posted doesn't compile as getemails is not declared. Is Ids supposed to be getemails ?
    – cropredy
    Jun 23, 2021 at 17:29
  • Yes Sorry I'm new in apex, Ids supposed to getEmails. Do you know what I'm doing wrong in this code?
    – Saaki
    Jun 23, 2021 at 17:45
  • please use edit to make the code posted correct; also use edit to show the loop, assignment, and apex action details (screen shots). Plus Summer 21 allows you to use the Debug option on record triggered flows and that is immensely useful at figuring out the issue with your flow
    – cropredy
    Jun 23, 2021 at 17:49
  • Thanks for the info. Sorry I meant the code is suppose to get all email addresses from collection and send out email to each one of them. Ids are still Emailaddresses. I'm not sure if thats the correct way to do it. I added the screen shots of Loop, assignments and Email action.
    – Saaki
    Jun 23, 2021 at 18:25

1 Answer 1

1

this should work:

public class SendEmailAction {
  @InvocableMethod(label='Send SE Email Notification') 
  public static void SendSENotification(List<List<String>> emailAddressCollections){
    
    List<OrgWideEmailAddress> lstEmailAddress=[SELECT Id 
                          FROM OrgWideEmailAddress 
                          WHERE Address='[email protected]'];
    List<EmailTemplate> SEEmailTemplate= [SELECT Id, Name, DeveloperName 
                           FROM EmailTemplate 
                           WHERE DeveloperName= 'Safety_Event_New_Event_Reported'];
         
    Messaging.SingleEmailMessage[] mails = new Messaging.SingleEmailMessage[0];
            
    for(List<String> emailAddresses : emailAddressCollections) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateId(SEEmailTemplate[0].Id);
        mail.setTargetObjectId(UserInfo.getUserId());
        mail.setToAddresses(emailAddresses);
        mail.setSaveAsActivity(false);
        mails.add(mail);
        mail.setOrgWideEmailAddressId(lstEmailAddress[0].Id);
    }
    Messaging.sendEmail(mails);
  }
}

I renamed your variables to be a bit clearer (as you are dealing with lists of lists)

Your flow looks OK as far as I can see. use the Summer 21 Record-Triggered Flow debugging to make sure that a collection of strings are passed in the flow interview to the invocable apex. If not, dig deeper into the Flow

4
  • Thank you, the code seems to be working via anonymous window but it still doesn't work with the flow, when i debug it passes the input as collection of email addresses [ [email protected], [email protected]....]. I doubt the apex parameter is accepting this type of collection. Perhaps, do you think we should pull the object Id's instead and inpur Ids as parameter and pull the email addresses via SOQL in Apex class and then send the Messaging.SingleEmailMessage?
    – Saaki
    Jun 24, 2021 at 16:26
  • well, I built a little flow that looped across some sobjects, building a text collection that I passed in to an invocable apex method just like yours. All the text collection elements were present. List<List<String>> is definitely supported.
    – cropredy
    Jun 25, 2021 at 1:13
  • Since this was a record trigger flow , I created another test schedule flow to debug little deeper. I built a text collection of email addresses and called the apex action. The flow failed with error" An Apex error occurred: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, The target object's email address "antonio.dabraio@00d4p000001dzqhuay" is not valid: [targetObjectId, 0054P00000Arw0WQAR]", I will ask another question if its going out of original question.
    – Saaki
    Jun 25, 2021 at 1:51
  • Answering my question re: target object's email address, I setTreatTargetObjectAsRecipient(false); and voila it worked!!. Thanks a ton
    – Saaki
    Jun 25, 2021 at 4:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .