When developing applications in Salesforce, writing clean and manageable code is crucial. One way to achieve this is by using design patterns, which are proven solutions to common coding problems. In this post, we’ll explore the Command Design Pattern—a useful pattern that can make your Apex code more flexible and easier to manage.

What is the Command Design Pattern?

The Command Design Pattern is a way to package an action, like inserting a record or sending an email, into an object. Think of it like putting a task in a box. This box (or command) can be stored, passed around, or executed whenever you need it.

This pattern is especially helpful when you need to:

  • Decouple: Separate the part of your code that decides to perform an action from the part that actually performs it.
  • Undo Actions: Easily reverse an action if needed.
  • Log Actions: Keep track of actions taken in your application.

Why Should You Use the Command Pattern in Salesforce Apex?

Salesforce development often involves complex business processes, and the Command Pattern helps you manage these processes more effectively. Here’s why you might want to use it:

  1. Simplify Your Code: By using commands, you can break down complex logic into smaller, manageable parts.
  2. Flexible Execution: You can choose when and how to execute commands, making your code more adaptable.
  3. Reusability: Once you create a command, you can use it in different parts of your application without rewriting code.

How to Implement the Command Pattern in Apex

Let’s go through a simple example of how you can use the Command Pattern in Salesforce Apex.

Step 1: Create a Command Interface

Start by creating an interface that all command objects will follow. This interface will have a method called execute() that will be used to perform the action.

public interface ICommand {
void execute();
}

Step 2: Make Command Classes

Next, create classes that implement the ICommand interface. Each class represents a specific action, like inserting an account or sending an email.

public class InsertAccountCommand implements ICommand {
private Account acc;

public InsertAccountCommand(Account acc) {
    this.acc = acc;
}

public void execute() {
    insert acc;
}

}

public class SendEmailCommand implements ICommand {
private Messaging.SingleEmailMessage email;

public SendEmailCommand(Messaging.SingleEmailMessage email) {
    this.email = email;
}

public void execute() {
    Messaging.sendEmail(new List<Messaging.SingleEmailMessage> {email});
}

}

Step 3: Create an Invoker

The invoker is like a manager that knows how to execute the commands. It can queue up commands and execute them one by one.

public class CommandInvoker {
private List commandQueue = new List();

public void addCommand(ICommand command) {
    commandQueue.add(command);
}

public void executeCommands() {
    for (ICommand command : commandQueue) {
        command.execute();
    }
}

}

Step 4: Put It All Together

Finally, let’s see how you can use these commands in your code. For example, you might want to insert an account and send an email in one go.

public class AccountService {
public void processAccount(Account acc, Messaging.SingleEmailMessage email) {
CommandInvoker invoker = new CommandInvoker();

    // Add commands to the invoker
    invoker.addCommand(new InsertAccountCommand(acc));
    invoker.addCommand(new SendEmailCommand(email));

    // Execute all commands
    invoker.executeCommands();
}

}

Wrapping Up

The Command Design Pattern is a simple but powerful tool that can help you write better Apex code. By using this pattern, you can make your code more flexible, easier to manage, and ready for changes. Whether you’re building a simple feature or a complex workflow in Salesforce, the Command Pattern can help you keep things organized.

I hope this guide has made the Command Design Pattern easy to understand. If you have any questions or want to share how you’ve used this pattern in your projects, feel free to leave a comment!

Vineet Yadav
Salesforce Blogger | Apex Enthusiast

By Vineet Yadav

I am a Salesforce Developer with over 11 years of IT experience, specializing in Apex, Lightning Web Components, and Salesforce integrations. I've worked with companies like HCL, Hitachi, Capgemini, and LTIMindtree, delivering impactful CRM solutions. My blog, thevineetyadav.com, offers tutorials and insights into Salesforce development to help others enhance their skills and careers.

Leave a Reply

Your email address will not be published. Required fields are marked *