DEV Community

Cover image for Advanced Apex Techniques for Salesforce Developers
Amr Saafan for Nile Bits

Posted on • Originally published at nilebits.com

Advanced Apex Techniques for Salesforce Developers

As Salesforce continues to dominate the Customer Relationship Management (CRM) space, the demand for highly skilled developers who can harness the power of Apex, Salesforce's proprietary programming language, is at an all-time high. Apex allows developers to create powerful customizations, automate processes, and integrate Salesforce with external systems. This blog post dives deep into advanced Apex techniques that can elevate your Salesforce development skills to the next level.

Why Mastering Advanced Apex is Crucial

To properly utilize Salesforce's potential, developers must become proficient in sophisticated Apex methods. Writing effective, scalable, and stable Apex code is essential as businesses depend more and more on Salesforce for sophisticated business operations. With advanced understanding of Apex, developers can:

Build complex, custom business logic that goes beyond the standard Salesforce features.

Optimize performance and resource usage within Salesforce's multi-tenant architecture.

Ensure the scalability of applications as data volumes and user interactions grow.

Maintain clean, reusable, and easily debuggable code.

In this post, we'll explore some of the most effective advanced Apex techniques, complete with practical examples and best practices.

Custom Metadata Types and Apex

Custom Metadata Types in Salesforce allow developers to create metadata configurations that can be packaged, deployed, and managed across different environments. Unlike custom objects, custom metadata records are treated as metadata, meaning they can be deployed alongside other customizations.

Using Custom Metadata Types in Apex allows you to build more dynamic and configurable applications. Here’s how you can leverage custom metadata in Apex:

Creating a Custom Metadata Type

First, let's create a simple Custom Metadata Type called Discount_Config_mdt with fields for Product_Categoryc and Discount_Percentage_c.

public class DiscountCalculator {
    public Decimal calculateDiscount(String productCategory) {
        Discount_Config__mdt config = [SELECT Discount_Percentage__c FROM Discount_Config__mdt WHERE Product_Category__c = :productCategory LIMIT 1];
        if (config != null) {
            return config.Discount_Percentage__c;
        }
        return 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we query the Custom Metadata Type to retrieve discount configurations based on the product category. This approach allows us to change discount rates without modifying the code—just update the Custom Metadata records.

Batch Apex and Asynchronous Processing

For operations that need to process large volumes of data, synchronous execution in Salesforce can lead to performance issues and exceed governor limits. Batch Apex provides a solution by enabling asynchronous processing, which splits large tasks into smaller, manageable chunks.

Writing a Batch Apex Class

Let’s create a Batch Apex class to update a large number of records. Consider the scenario where you need to apply a discount to all products in a specific category:

global class ProductDiscountBatch implements Database.Batchable<SObject> {

    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('SELECT Id, Price__c FROM Product__c WHERE Category__c = \'Electronics\'');
    }

    global void execute(Database.BatchableContext BC, List<Product__c> scope) {
        for (Product__c prod : scope) {
            prod.Price__c = prod.Price__c * 0.9;
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC) {
        System.debug('Batch process completed successfully.');
    }
}
Enter fullscreen mode Exit fullscreen mode

To execute this Batch Apex:

ProductDiscountBatch batch = new ProductDiscountBatch();
Database.executeBatch(batch, 200);
Enter fullscreen mode Exit fullscreen mode

This code reduces the price of all products in the "Electronics" category by 10%. Batch Apex allows Salesforce to handle this operation efficiently, even with a large number of records.

Apex Design Patterns

Design patterns are a crucial aspect of advanced programming, helping to structure code for better maintainability, scalability, and flexibility. In Apex, some of the most commonly used design patterns include Singleton, Factory, and Strategy patterns.

Singleton Pattern

The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is useful in scenarios where a single object needs to coordinate actions across the system.

public class DiscountManager {
    private static DiscountManager instance;

    public Decimal discountRate { get; set; }

    private DiscountManager() {
        discountRate = 0.1;
    }

    public static DiscountManager getInstance() {
        if (instance == null) {
            instance = new DiscountManager();
        }
        return instance;
    }
}
Enter fullscreen mode Exit fullscreen mode

You can now retrieve and use the DiscountManager instance:

DiscountManager manager = DiscountManager.getInstance();
Decimal rate = manager.discountRate;
Enter fullscreen mode Exit fullscreen mode

This pattern ensures that only one DiscountManager object exists, reducing memory overhead and ensuring consistency.

Factory Pattern

The Factory pattern is useful when you need to create objects without specifying the exact class of the object that will be created. This is particularly helpful in scenarios where you need to instantiate objects based on dynamic inputs.

public class ProductFactory {
    public static Product createProduct(String type) {
        if (type == 'Electronics') {
            return new ElectronicsProduct();
        } else if (type == 'Clothing') {
            return new ClothingProduct();
        } else {
            return new GeneralProduct();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, you can create specific product instances based on the type:

Product newProduct = ProductFactory.createProduct('Electronics');

This approach abstracts the instantiation logic, making it easier to manage and extend.

Dependency Injection in Apex

Dependency Injection (DI) is a design pattern that allows a class to receive its dependencies from an external source rather than creating them internally. This promotes loose coupling and enhances testability.

Implementing Dependency Injection

Consider a scenario where you need to send notifications through different channels (email, SMS). You can create an interface INotification and implement specific classes:

public interface INotification {
    void send(String message);
}

public class EmailNotification implements INotification {
    public void send(String message) {
        // Logic to send email
    }
}

public class SMSNotification implements INotification {
    public void send(String message) {
        // Logic to send SMS
    }
}
Enter fullscreen mode Exit fullscreen mode

You can inject the dependency through the constructor or a setter method:

public class NotificationService {
    private INotification notifier;

    public NotificationService(INotification notifier) {
        this.notifier = notifier;
    }

    public void notifyUser(String message) {
        notifier.send(message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage:

INotification emailNotifier = new EmailNotification();
NotificationService service = new NotificationService(emailNotifier);
service.notifyUser('Hello, User!');
Enter fullscreen mode Exit fullscreen mode

This technique allows you to swap out the notification implementation without modifying the NotificationService class.

Using Custom Settings for Configurations

Custom Settings in Salesforce provide a way to store custom data that can be accessed across your organization. Unlike custom objects, they are primarily used for application configurations and preferences.

Hierarchy Custom Settings

Hierarchy Custom Settings allow you to define values at the organization, profile, or user level. For example, you might want to store different discount rates for different user profiles:

public class DiscountService {
    public static Decimal getDiscountRate() {
        UserInfo currentUser = UserInfo.getUserInfo();
        DiscountSetting__c settings = DiscountSetting__c.getInstance(currentUser.getProfileId());
        return settings != null ? settings.Discount_Rate__c : 0.1;
    }
}
Enter fullscreen mode Exit fullscreen mode

This approach lets you apply different configurations based on the current user's profile, enhancing the flexibility of your Salesforce applications.

Leveraging Apex for Integration

Salesforce often needs to integrate with external systems, and Apex provides robust tools to handle these integrations via REST and SOAP web services.

Creating a REST API with Apex

You can expose Apex classes as RESTful web services by using the @RestResource annotation. Here’s an example of a simple REST API that retrieves product details:

@RestResource(urlMapping='/products/*')
global with sharing class ProductAPI {

    @HttpGet
    global static Product__c getProductDetails() {
        RestRequest req = RestContext.request;
        String productId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        return [SELECT Id, Name, Price__c FROM Product__c WHERE Id = :productId LIMIT 1];
    }
}
Enter fullscreen mode Exit fullscreen mode

To access this API, you would make a GET request to /services/apexrest/products/{productId}.

Handling Large Data Volumes with Queueable Apex

Queueable Apex allows you to submit jobs for asynchronous processing and is particularly useful for handling large data volumes or long-running operations. Queueable Apex is more flexible than future methods and provides more features, such as job chaining.

Example of Queueable Apex

public class ProductPriceUpdater implements Queueable {
    public void execute(QueueableContext context) {
        List<Product__c> products = [SELECT Id, Price__c FROM Product__c WHERE Category__c = 'Electronics'];
        for (Product__c prod : products) {
            prod.Price__c = prod.Price__c * 0.95;
        }
        update products;
    }
}
Enter fullscreen mode Exit fullscreen mode

To enqueue this job:

ProductPriceUpdater updater = new ProductPriceUpdater();
ID jobId = System.enqueueJob(updater);
Enter fullscreen mode Exit fullscreen mode

Queueable Apex provides more control over the job, including tracking job status and chaining additional jobs.

Conclusion

To create dependable, scalable, and stable Salesforce apps, advanced Apex approaches are necessary. Salesforce developers may build strong solutions that satisfy challenging business requirements by becoming proficient in ideas like Custom Metadata Types, Batch Apex, design patterns, Dependency Injection, and integration patterns. Maintaining up to speed with these cutting-edge methods will guarantee that your abilities stay applicable and in-demand as Salesforce continues to develop.

For more detailed information, you can refer to Salesforce's official Apex Developer Guide and other resources such as the Salesforce Stack Exchange community.

Top comments (0)