+ Start a Discussion
SoundarSoundar 

Variable does not exist: EmailManager

 Dear Friends,

Can Anyone explain Why this error was showing here ...

trigger ExampleTrigger on Contact (after insert, after delete) {
    if (Trigger.isInsert) {
        Integer recordCount = Trigger.New.size();
        // Call a utility method from another class
        EmailManager.sendMail('Your email address', 'Trailhead Trigger Tutorial', 
                    recordCount + ' contact(s) were inserted.');

    }
    else if (Trigger.isDelete) {
        // Process after delete
    }
}


Error : Variable does not exist: EmailManager


Thanks In Advance ..
Best Answer chosen by Soundar
Malni Chandrasekaran 2Malni Chandrasekaran 2
Soundar Rajan,
Please try changing the sendMail method signature to :

public static void sendMail(String address, String subject, String body)

If you have to invoke a method (from trigger in your case) without instantiating an object, it should be defined as Static.

If you dont want to define it as Static, please create an instance (object) for the class EmailManager and then invoke the method sentMail.
Eg:
EmailManager obj = new EmailManager();
obj.sendMail('Your email address', 'Trailhead Trigger Tutorial', 
                    recordCount + ' contact(s) were inserted.');

Hope this helps.
Please mark it as solved if it answers your question.
 

All Answers

SoundarSoundar
Dear Friends,

I realised My Mistakes here .. there is no class with a name of EmailManager  Now i have saved This class as below...



=========================================================================

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

}

\

Now It's Showing Below Error : 

Method is not visible: EmailManager.sendMail(String, String, String)




Thnaks in Advance ..
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Soundar Rajan,
Please try changing the sendMail method signature to :

public static void sendMail(String address, String subject, String body)

If you have to invoke a method (from trigger in your case) without instantiating an object, it should be defined as Static.

If you dont want to define it as Static, please create an instance (object) for the class EmailManager and then invoke the method sentMail.
Eg:
EmailManager obj = new EmailManager();
obj.sendMail('Your email address', 'Trailhead Trigger Tutorial', 
                    recordCount + ' contact(s) were inserted.');

Hope this helps.
Please mark it as solved if it answers your question.
 
This was selected as the best answer
SoundarSoundar
Malni Chandrasekaran ,

Yeah Absolutely Correct ... Thansk for your quick support .
 
Sid ali Abid 5Sid ali Abid 5
the goal of this part of test is to use the class EmailManager that we created in the fist part of trailhead training so as you use an other thrailhead playground :) ,so if you create the same class that we use on "Get started with Apex" it will works :)
Veronica RebenkoVeronica Rebenko
You forget to modify the email address placeholder text 'Your email address' to your valid email address.
sultan Subhansultan Subhan
first create a public class EmailManager and add the sendMail method to it!!