Module 3: Creating an Apex Class

Apex is a strongly typed, object-oriented programming language that you use to execute code in your Saleforce instance. The Apex syntax is similar to Java and also includes built-in support for database operations. In this module, you create an EmailManager class that encapsulates the logic to send confirmation emails to the conference speakers.

Step 1: Create the EmailManager class

  1. In Salesforce, click your name in the upper right corner of the screen. In the dropdown menu, click Developer Console.

  2. In the Developer Console, click File > New > Apex Class. Specify EmailManager as the class name and click OK

  3. Implement the class as follows:

    public class EmailManager {
    
        public void sendMail(String address, String subject, String body) {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {address};
            mail.setToAddresses(toAddresses);
            mail.setSubject(subject);
            mail.setPlainTextBody(body);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    
    }
    
  4. Click File > Save to save the file

Step 2: Send an Email

In this module, you test the EmailManager class by sending an email from the developer console. Later in this workshop, you'll integrate the EmailManager class with other parts of the application to automate the process of sending confirmation emails.

  1. In the Developer Console, click Debug > Open Execute Anonymous Window

  2. Type the following Apex code (provide your own email address):

    String address = 'YOUR_EMAIL_ADDRESS';
    String subject = 'Speaker Confirmation';
    String body = 'Thank you for speaking at the conference.';
    EmailManager em = new EmailManager();
    em.sendMail(address, subject, body);
    
  3. Click the Execute button

  4. Check your email: you should have received the confirmation email

Step 3: Using a Static Method

Since EmailManager is a utility class that doesn't use instance-specific variables, you can make the sendMail() method static:

  1. In the Developer Console, open the EmailManager class

  2. To turn sendMail() into a static method, change its signature as follows (add the static keyword):

    public static void sendMail(String address, String subject, String body) {
    
  3. Save the file

  4. Go back to the Execute Anonymous Window (Debug > Open Execute Anonymous Window)

  5. Modify the Apex code to invoke sendMail() using a static method call:

    String address = 'YOUR_EMAIL_ADDRESS';
    String subject = 'Speaker Confirmation';
    String body = 'Thank you for speaking at the conference.';
    EmailManager.sendMail(address, subject, body);
    
  6. Click Execute and check your email

    if you get a Method does not exist or incorrect signature error, make sure you made the sendMail() method static (Step 3 / point 2 above).

comments powered by Disqus