Writing Batchable, Schedulable Apex Code

Introduction

This is critical knowledge for any Salesforce developer to understand and understand well. I personally could have avoided a lot of unforeseen errors in the past if I had only learned earlier when and how to properly utilize batch Apex!

From an administrator’s perspective, I also prefer to use scheduled classes to do things such as ensure data quality. In my mind, something simple like the example in the video advances usability and cleanliness a lot, but isn’t necessarily they type of functionality which would warrant a trigger. Instead, it’s the type of code which could run daily or even weekly and still prove really useful to the end users.

In general, the use of batch classes helps you avoid errors having to do with the insertion and update of records en masse. In addition, the use of reusable utility functions helps a developer separate and silo functionality and avoid repeating themselves.

Video Breakdown

This video will teach you how to do the following:

  1. Write a utility function that can be used in other Apex classes throughout your Salesforce Organization.
  2. Write a batchable, schedulable class which utilizes that utility function.
  3. Do a quick test to ensure the class works, then schedule that class to run on a daily/weekly/monthly basis.
  4. Monitor the executions of that scheduled batch class to confirm it is working correctly!

Code Samples

Here’s the code utilized in the video above:

Lead Utility Class and Function
global class LeadUtilities {

    global static String capitalizeName (String unformattedName) {
        /* Turn a name like john DOE into John Doe. */
        List nameParts = unformattedName.split('(\\s| |\\-|\\.){1,}');
        List cleanNameParts = new List();
        for (String n: nameParts) {
            String clean = n.toLowerCase();
            String cleanCap = clean.capitalize();
            cleanNameParts.add(cleanCap);
        } return String.join(cleanNameParts, ' ');
    }
        
}
Batch Class Definition
global class BatchCapitalizeNames implements Schedulable, Database.Batchable {

    global Database.QueryLocator start (Database.BatchableContext BC) {
        String query = 'SELECT Id, FirstName, LastName FROM Lead WHERE CreatedDate = LAST_N_DAYS:7';
        return Database.getQueryLocator(query);
    }
    
    global void execute (Database.BatchableContext BC, List<Lead> scope) {
                
        for (Lead l: scope) {
            l.FirstName = LeadUtilities.capitalizeName(l.FirstName);
            l.LastName = LeadUtilities.capitalizeName(l.LastName);
        }
        
        try {
            update scope;
        } catch (Exception e) {
            System.debug(e.getMessage());
        }
        
    }
    
    global void finish (Database.BatchableContext BC) {
		// Leave this blank in this example! Not necessary.
    }
    
    global void execute (SchedulableContext SC) {
		Database.executeBatch(new BatchCapitalizeNames());
    }
    
}

That’s all for this one, thanks for watching and reading!

Add comment

Recent Posts

Categories