• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

Part IV. 2. Apex Class Creation in Salesforce

  • November 2, 2017
  • Merfantz Editor
  • Salesfore Training Course, 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.

 

Author Bio

Merfantz Editor
+ Recent Posts
  • August 4, 2023
    How to Setup CRM Analytics for Financial Services Cloud
  • August 2, 2023
    How to Get Started with Salesforce Hyperforce - Merfantz
  • July 28, 2023
    Health Cloud Mastery: Salesforce Trailhead for Effective Healthcare CRM
  • July 26, 2023
    How to integrate Data cloud and Marketing cloud In Salesforce
  • Previous Part IV. 1. Page Creation in Salesforce
  • Next Part IV. 3. SOQL Basics in Salesforce
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025
Einstein Activity Capture Boosting Productivity & Relationship Intelligence
Einstein Activity Capture: Boosting Productivity & Relationship Intelligence May 22, 2025
Let Einstein Help You Build - Salesforce Flow
Let Einstein Help You Build – Salesforce Flow May 15, 2025

Copyright @2023 Merfantz Technologies, All rights reserved