function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aron Schor [Dev]Aron Schor [Dev] 

Apex Trigger: Method does not exist or incorrect signature: MailManager.SendMail(String, String, String)

Hi,

I get that error when I try to save this code.

trigger ExampleTrigger2 on Contact (after insert, after delete) {
    if (Trigger.isInsert) {
        Integer recordCount = Trigger.New.size();
        // Call a utility method from another class
        EmailManager.sendMail('aschor@acmeunited.com', 'Trailhead Trigger Tutorial', 
                    recordCount + ' contact(s) were inserted.');
    }
    else if (Trigger.isDelete) {
        // Process after delete
    }
}

thanks,
 

Aron

Best Answer chosen by Aron Schor [Dev]
Amit Chaudhary 8Amit Chaudhary 8
Please modify you class like below :-
 
public class EmailManager {
 
    // Public method
    public static void sendMail(String address, String subject, String body) 
	{
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
         
        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
     
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
         
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results.
        // In this class, the methods send only one email,
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
           }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                
            }
        }
        
        return sendResult;
    }

}

Please let us know if this will help you

All Answers

smitrasmitra
Please check the signature of static method present in EmailManager class .The data type and number of parameters should match with the invoking method present in this trigger
Aron Schor [Dev]Aron Schor [Dev]
Can you clarify what I need to do?  I am a bit of a newbie with this.  Here is the class.

public class EmailManager {
 
    // Public method
    public void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
         
        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
     
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
         
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results.
        // In this class, the methods send only one email,
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
           }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                
            }
        }
        
        return sendResult;
    }

}
smitrasmitra
first of all the method 
sendMail() has to be declared as static ..try this one


public static sendMail(String address, String subject, String body)
 
Aron Schor [Dev]Aron Schor [Dev]
If I change the class to
public static sendMail(String address, String subject, String body) {
from
public void sendMail(String address, String subject, String body) {

It says
Error Error: Compile Error: Constructors cannot be static at line 4 column 19
Amit Chaudhary 8Amit Chaudhary 8
Please modify you class like below :-
 
public class EmailManager {
 
    // Public method
    public static void sendMail(String address, String subject, String body) 
	{
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
         
        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
     
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
         
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results.
        // In this class, the methods send only one email,
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
           }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                
            }
        }
        
        return sendResult;
    }

}

Please let us know if this will help you
This was selected as the best answer
Aron Schor [Dev]Aron Schor [Dev]
Thanks, its working!
SahajSahaj
Hi All,

Can you please elaborate why do we need to change the method to static.
Thanks in advance.
Swapnadip Chakraborty 6Swapnadip Chakraborty 6
Hi Sahaj, We have to change the method name to static because in the trigger we are invoking SendMail method without instantiating class EmailManager. For static methods we can simply call the method by classname.methodname
Julie WorkmanJulie Workman
This issue is addressed in the following Trailhead module: https://trailhead.salesforce.com/apex_database/apex_database_intro 
Chandra ShekharChandra Shekhar
Hi all, 
  what if i change 
public static void sendMail(String address, String subject, String body)
to 
public static void sendMail(String[] address, String subject, String body)

then there is no need of writing this below line 
String[] toAddresses = new String[] {address};
and directly i can write
mail.setToAddresses(address);

but on testing it is showing error. Is function signatur of " sendMail" is of this type 
public static void sendMail(String address, String subject, String body)

by default in apex


 
Sakshi Dhakad 8Sakshi Dhakad 8
Can this error be resolved in this Trailhead as well? - https://trailhead.salesforce.com/apex_triggers/apex_triggers_intro 
Thanks!
shanmukh prasadshanmukh prasad
Instead of changing the method signature in EmailManager better create Object for EmailManager and call the method like below:
 EmailManager emailManger = new EmailManager();
        emailManger.sendMail('mail id', 'Trailhead Trigger Tutorial',
                    recordCount + ' contact(s) were inserted.');
SSKKPP82SSKKPP82
So I faced the same issue today and was able to resolve it. Don't know if it makes sense technically, but it led to a successful result.

While copying the code from trailhead to developer console, the blank lines get numbered automatically. I am extremely new to coding, so I not sure why they showed up automarically or if there is a way to override/ignore those.

But after I removed those numbers, the code ran fine and the email was sent. Did not have to make any changes to public void declarations.
 
SFDC@BossSFDC@Boss
I keep getting this error when try I to insert a record for the same above example:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ExampleTrigger: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, ID is invalid or you do not have access to the record.: [toAddresses, Your email address] Class.EmailManager1.sendMail: line 12, column 1 Trigger.ExampleTrigger: line 5, column 1: []
 
Rizwan Ali 8Rizwan Ali 8
I have this error :
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, ID is invalid or you do not have access to the record.: [toAddresses, Your email address]
Rizwan Ali 8Rizwan Ali 8
Ohhh , I got it 

I have to type my Email Address instead of  'Your email address' in Execute Anonymous Window .
Jitesh RaiJitesh Rai
Hi All,
Make sure you save your code before you run it and make this line of code static. It's missing static from what is given in trailheads.
"Public static  void sendMail(String address, String subject, String body)" Also make sure to update the line "your email address" while executing to your own email address and that should work. If not lmk
Abhishek Jain 30Abhishek Jain 30
Hi All,
I am getting below error while compiling:

Static method cannot be referenced from a non static context: void EmailManager.sendMail(String, String, String)
Adrianne Geyer 2Adrianne Geyer 2
There is no need to declare a public static method. The sendMail() method can be called directly on the class name like this:
EmailManager.sendMail(addresses, subjects, messages);

Comment or delete the following code because it is instantiating the EmailManage class.
EmailManager em = new EmailManager();
em.sendMail(addresses, subjects, messages);