In this intermediate-level Apex tutorial, we will cover more advanced concepts, such as interfaces, inheritance, batch processing, schedulable classes, and callouts. We will also provide code samples for each topic.
- Interfaces
An interface is a collection of method signatures (without implementations) that can be implemented by any class. Interfaces enable you to define a common set of methods that different classes can implement in their own way. Here’s an example of an interface and a class that implements it:
public interface Animal { String speak(); } public class Dog implements Animal { public String speak() { return 'Woof!'; } } public class Cat implements Animal { public String speak() { return 'Meow!'; } }
You can use the interface to create instances of the implementing classes and call their methods:
Animal dog = new Dog(); System.debug(dog.speak()); // Output: Woof! Animal cat = new Cat(); System.debug(cat.speak()); // Output: Meow!
- Inheritance
In Apex, you can create subclasses that inherit properties and methods from a superclass. This allows you to create a hierarchy of classes and reuse code. Here’s an example of inheritance:
public virtual class Shape { public String getName() { return 'Shape'; } } public class Circle extends Shape { public override String getName() { return 'Circle'; } } public class Square extends Shape { public override String getName() { return 'Square'; } }
In this example, Circle
and Square
classes extend the Shape
class and override the getName
method. You can create instances of these classes and call their methods:
Shape circle = new Circle(); System.debug(circle.getName()); // Output: Circle Shape square = new Square(); System.debug(square.getName()); // Output: Square
- Batch Processing
When you need to process a large number of records, you can use Batch Apex. This allows you to process data in smaller chunks, known as batches, to avoid hitting governor limits. To create a Batch Apex class, you need to implement the Database.Batchable
interface and its three methods: start
, execute
, and finish
. Here’s an example:
public class UpdateAccountRatingBatch implements Database.Batchable<sObject> { public Database.QueryLocator start(Database.BatchableContext context) { return Database.getQueryLocator('SELECT Id, Rating__c FROM Account'); } public void execute(Database.BatchableContext context, List<Account> scope) { for (Account acc : scope) { acc.Rating__c = 'Updated'; } update scope; } public void finish(Database.BatchableContext context) { System.debug('Batch processing complete.'); } }
To execute the batch, you can use the following code:
UpdateAccountRatingBatch batch = new UpdateAccountRatingBatch(); ID batchId = Database.executeBatch(batch, 200); // Execute the batch with a batch size of 200
- Schedulable Classes
You can schedule Apex classes to run at specific times by implementing the Schedulable
interface. Here’s an example of a schedulable class that updates all Account records:
public class UpdateAccountRatingScheduler implements Schedulable { public void execute(SchedulableContext context) { UpdateAccountRatingBatch batch = new UpdateAccountRatingBatch(); Database.executeBatch(batch, 200); } }
Salesforce Apex security best practices
Working with Time in Salesforce Apex
Schedule Apex in Salesforce: A Technical Guide for Developers
Speeding Up Apex Test Execution: A Guide for Faster Salesforce Deployments