Module 4: 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 static 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.sendMail(address, subject, body);
    
  3. Click the Execute button

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

comments powered by Disqus