I have a problem writing an Apex class which will send the emails to the list of Cases based on specified criteria from scheduled flow. Here is my code:

public class EmailClass
    public static void sendEmail();
    List<Contact> contacts =[Select Id From Contact where Email!=null];
    List<Case> cases = [Select Id,ContactEmail, ContactId From Case Where Follow_Up_Date_in_Days__c=7 AND ContactEmail!=null AND status='New' AND ContactId IN : contacts];
    { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

Error messages:

  • Line 22 Method must have a body
  • Line 28 Method does not exist or incorrect signature: void setTargetObjectId(List) from the type Messaging.SingleEmailMessage

I don't understand where I make a bug...

Can someone please let me know?

  • 1
    The code that you provided only has 12 lines, so we cannot say anything about lines 22 and 28. Please provide the full code. Sep 1, 2022 at 11:10

1 Answer 1


1sr error: Your invocable method (sendEmail) hasn't been defined correctly - a method needs curly braces immediately after the name and signature like so:

public static void sendEmail() {}

All the code for that method needs to be between the curly braces.

2nd error: you have to construct each instance of SingleEmailMessage individually, add them to a List then use the Messaging.sendEmail method on the list - see the accepted answer to this question for an example: Bulkification of SingleEmailMessage

You must log in to answer this question.

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