[SalesForce] Execute Anonymous Error: Getting Started with Apex

I am working through the trail head module and have copied the code to create an APEX class to send emails like for like.

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;
    }

}

The instructions say to Anonymously execute it with this:

EmailManager em = new EmailManager();
em.sendMail('My Email Address', 'Trailhead Tutorial', '123 body');

Upon execution , I get the following error:

Execute Anonymous Error

Line: 2, Column: 4 Method does not exist or incorrect signature: void
sendMail(String, String, String) from the type EmailManager

New to APEX and this is copied verbatim, so confused as to why it isnt working

Best Answer

Seems 100% like a your EmailManager failed to compile. Normally you would see errors similar to this one if you declared your method static and called it the way you did:

// won't work, can't call static method from instance
public class MyClass
{
    public static void myMethod(/*params*/) { /*implementation*/ }
}

// script
new MyClass().myMethod(/*params*/);

Or if you declared it an instance method and tried to call it statically:

// won't work, can't call instance method statically
public class MyClass
{
    public void myMethod(/*params*/) { /*implementation*/ }
}

// script
MyClass.myMethod(/*params*/);
Related Topic