• WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Implementations
        • Salesforce
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service for SMEs
          • Field Service Lightning
        • Salesforce Partner Apps
          • Panda Doc
          • Conga
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • HOW WE DO
      • Delivery Model
      • Our Works
    • BLOG
Salesforce Blog | Salesforce Simple Tutorial
Salesforce Blog | Salesforce Simple Tutorial
  • WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • BLOG

Part IV. 2. Apex Class Creation in Salesforce

Salesforce Blog | Salesforce Simple Tutorial > SFDC Training > SFDC Training Part IV > Part IV. 2. Apex Class Creation in Salesforce
  • November 2, 2017
  • Merfantz Editor
  • SFDC Training, SFDC Training Part IV
  • 0

In this Training, you create an EmailManager class that encapsulates the logic to send confirmation emails to the conference speakers.

Step 1: Create the EmailManager class

1.Click Setup Icon and select Developer Console.

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

3.Paste the following into the Developer Console.

public with sharing class EmailManager{
public void sendMail(String [] addresses, String [] subjects, String [] messages) {
Messaging.SingleEmailMessage [] emails = new Messaging.SingleEmailMessage[]{};
Integer totalMails = addresses.size();
for(Integer i=0; i < totalMails; i++){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject(subjects[i]);
email.setToAddresses(new List<String> { addresses[i] });
email.setPlainTextBody(messages[i]);
emails.add(email);
}
Messaging.sendEmail(emails);
}
}

4.Click File > Save to save the file.

Step 2: Send an Email

In this step, you test the EmailManager class by sending an email from the Developer Console. Later in this project, you 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.’;
String[] addresses = new String[]{},
subjects = new String[]{},
messages = new String[]{};
addresses.add(address);
subjects.add(subject);
messages.add(body);
EmailManager em = new EmailManager();
em.sendMail(addresses, subjects, messages);

4.Click the Execute button.

5.Check your email. You should have received a confirmation email.

Step 3: Using a Static Method

Since EmailManager is a utility class that doesn’t work with 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[] addresses, String[] subjects, String[] messages) {

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() statically (provide your own email address).

String address = ‘YOUR_EMAIL_ADDRESS’;
String subject = ‘Speaker Confirmation’;
String body = ‘Thank you for speaking at the conference.’;
String[] addresses = new String[]{},
subjects = new String[]{},
messages = new String[]{};
addresses.add(address);
subjects.add(subject);
messages.add(body);
EmailManager.sendMail(addresses, subjects, messages);

Click Execute and check your email.

Please refer the following link to learn more about Apex class programming How to get geolocation through apex class 

We are the ISV Partners and Please reach us for custom development => www.merfantz.com

This training will be very useful to learn about Apex class creation in salesforce.

 


  • Previous Part IV. 1. Page Creation in Salesforce
  • Next Part IV. 3. SOQL Basics in Salesforce
We denounce with righteous indignation in and dislike men who are so beguiled and to demo realized by the charms of pleasure moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound.

Discover More

Contact Info

  • 374 William S Canning Blvd, Fall River MA 2721, USA
  • (+880)155-69569
  • [email protected]
  • 10:00 - 17:00

Latest Posts

Salesforce Marketing Cloud Will Fuel Your B2B Marketing Engine. January 30, 2023
Overview of DML operations in Apex January 24, 2023
Benefits of Integrating Salesforce and Marketing Automation for Business Growth January 20, 2023

2023 All Rights Reserved