+ Start a Discussion

Getting Started with Apex Code Exercise Error

I am doing the exercise portion of the Trailhead module and I copies and pasted the first Apex code into the Developer console and saved it. When I went to to the debug Execute Anonymous Window and enter the following. I got the below error message.

EmailManager em = new EmailManager();
em.sendMail('Your email address', 'Trailhead Tutorial', '123 body');

Line: 13, Column: 1
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]

Here is the orginal code from the exercise that I copy and pasted into the Dev Console
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};
        // 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
    // 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;
SFDC HedgehogSFDC Hedgehog
Hi Rejones,
I know I'm coming late to this, but your code seems to work perfectly.

I got the email just fine with the code you wrote and there was no error in the debug log.

User-added image

..-Could it be your email address you entered drom the debug console was badly formatted?  Or the text message wasn't UTF-8?
Sandesh Jaitapkar99Sandesh Jaitapkar99
You need to replace "Your email address" with a valid email id. 
Prem Kumar 70Prem Kumar 70
In your trigger where you are calling 'sendMail' method of 'EmailManager' class - you need to replace 'Your email address' with a valid email Id in method parameter.