• 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. 7. Batch Class Creation in Salesforce

  • November 2, 2017
  • Merfantz Editor
  • Salesfore Training Course, SFDC Training Part IV
  • 0

In this training, you create and execute a batch class.

Batch Apex

Batch Apex is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits. Using Batch Apex, you can process records asynchronously in batches (hence the name, “Batch Apex”) to stay within platform limits. If you have a lot of records to process, for example, data cleansing or archiving, Batch Apex is probably your best solution.

Step 1: Create the Batch Class

  1. In the Developer Console, select File > New > Apex Class, specify SendReminderEmail as the class name and click OK.
  2. Make the class global, implement the Batchable interface, and define the three methods of the interface.

global class SendReminderEmail implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
}
global void execute(Database.BatchableContext bc, List<Speaker__c> scope) {
}
global void finish(Database.BatchableContext bc) {
}
}

3.Declare three instance variables to store the query, the email subject, and the email body.

global String query;
global String subject;
global String body;

4.Define a constructor implemented as follows.

global SendReminderEmail(String query, String subject, String body) {
this.query = query;
this.subject = subject;
this.body = body;
}

5.Implement the start() method as follows.

global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}

6.Implement the execute() method as follows.

global void execute(Database.BatchableContext bc, List<Speaker__c> scope) {
String[] addresses = new String[]{},
subjects = new String[]{},
messages = new String[]{};
for (Speaker__c speaker : scope) {
addresses.add(speaker.Email__c);
subjects.add(this.subject);
messages.add(this.body);
}
EmailManager.sendMail(addresses, subjects, messages);
}

7.Save the file.

Step 2: Run the Batch

  1. Make sure you have assigned your own email address to one of the speakers.
  2. In the Developer Console, click Debug > Open Execute Anonymous Window.
  3. Type the following Apex code.

String q = ‘SELECT First_Name__c, Last_Name__c, Email__c FROM Speaker__c WHERE Email__c != null’;
SendReminderEmail batch = new SendReminderEmail(q, ‘Final Reminder’, ‘The conference starts next Monday’);
Database.executeBatch(batch)

4.Click Execute.

5.Check your email.

This Training is very useful to learn about batch class in salesforce.

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

 

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. 6. Components Creation in Salesforce
  • Next Part IV. 8. Lightning 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

Mastering Attachment Compression for Salesforce Developers May 9, 2025
Salesforce API Integrations: Connect with Slack, Zoom, and Teams May 2, 2025
Guide to Streamlining Sales Success
Mastering Salesforce CPQ: A Comprehensive Guide to Streamlining Sales Success April 17, 2025

Copyright @2023 Merfantz Technologies, All rights reserved