Level Up Your Salesforce Code: Building an Advanced Generic Apex Helper Class

 

🚀 Introduction

As Salesforce projects grow, developers need robust, reusable utilities to handle common tasks like data processing, error handling, and logging. In this post, we'll build an Advanced Generic Apex Helper Class—a toolkit designed for bulk operations, safe data manipulation, and scalable automation. Whether you're working on triggers, batch jobs, or custom services, these patterns will help you write cleaner, more maintainable code.

🧩 Why Use a Helper Class?

  • Reusability: Centralizes common logic, reducing code duplication
  • Maintainability: Makes updates and bug fixes much simpler
  • Scalability: Handles bulk data and large processes with ease
  • Consistency: Ensures standardized error handling and logging across your org

🛠️ The AdvancedHelper Apex Class

Here’s a sample class with several advanced utilities you can use and extend:

public class AdvancedHelper { // Bulk update a field for a list of SObjects public static void updateField(List<SObject> records, String fieldName, Object value) { for (SObject rec : records) { rec.put(fieldName, value); } } // Merge two maps safely (second map overwrites first for matching keys) public static Map<Id, SObject> mergeMaps(Map<Id, SObject> map1, Map<Id, SObject> map2) { Map<Id, SObject> merged = new Map<Id, SObject>(map1); for (Id key : map2.keySet()) { merged.put(key, map2.get(key)); } return merged; } // Custom logging with log levels public static void log(String level, String message) { System.debug('[' + level + '] ' + message); // Extend: write logs to a custom object if needed } // Try/catch wrapper for safe execution of code blocks public static void tryCatch(VoidMethod method) { try { method.invoke(); } catch (Exception e) { log('ERROR', 'Exception: ' + e.getMessage()); } } // Example interface for passing code blocks public interface VoidMethod { void invoke(); } // Filter SObjects by field value public static List<SObject> filterByValue(List<SObject> records, String fieldName, Object value) { List<SObject> filtered = new List<SObject>(); for (SObject rec : records) { if (rec.get(fieldName) == value) { filtered.add(rec); } } return filtered; } }

🎯 Practical Examples

Bulk Field Updates (Triggers & Batches):

AdvancedHelper.updateField(accounts, 'Industry', 'Consulting');

Safe Execution & Logging (Services):

AdvancedHelper.tryCatch(new AdvancedHelper.VoidMethod() { public void invoke() { // risky code here } });

Filter Records by Criteria:

List
<Account> techAccounts = (List<Account>)AdvancedHelper.filterByValue(accounts, 'Industry', 'Technology');

💡 Pro Tips for Helper Classes

  • Extend logging to custom objects for persistent error tracking
  • Add generic methods for SOQL query building and dynamic DML
  • Use interfaces for flexible, testable code

🏆 Conclusion

An advanced generic helper class is a game-changer for any Salesforce developer. It streamlines your automation, enforces best practices, and sets you up for long-term success in complex orgs. Start building your helper library today and watch your productivity soar!

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post