Efficient Debugging in Apex: A Guide to Using the DebugUtil Class

In the world of Salesforce development, efficient debugging is crucial. The DebugUtil class is a powerful tool that helps capture and print debug messages effectively. Let’s dive into how you can leverage this class for streamlined debugging.

public with sharing class DebugUtil {
 
    private static List<String> messages = new List<String>();
    private static Integer count = 1;
 
    public static void addMessage(Object message) {
        if(message != null) {
            messages.add('Step ' + count++ + ' : Line ' + currentLineNumber() + ' ' + String.valueOf(message) );
        }
    }
 
    public static void printAssert() {
        if(!messages.isEmpty() && isDebuggingEnabled()){
            System.assert(false, String.join(messages, ', '));
            clearMessages();
        }
    }
 
    private static void clearMessages() {
        messages.clear();
        count = 0;
    }

    private static Boolean isDebuggingEnabled() {
        // Query the custom setting for the specific record name 'Admin Settings'
        ERx_Forms__FormBuilder_Settings__c adminSettings = [SELECT ERx_Forms__Debugging__c FROM ERx_Forms__FormBuilder_Settings__c WHERE Name = 'Admin Settings' LIMIT 1];

        // Return the value of the ERx_Forms__Debugging__c field
        return adminSettings.ERx_Forms__Debugging__c;
    }

    public static Integer currentLineNumber() {
        try {
            Integer x = 0 / 0;
        } catch(Exception e) {
            String line2 = e.getStackTraceString().split(' line ')[3].split(',')[0];
            return Integer.valueOf(line2);
        }
        return null;
    }
}

Features of DebugUtil Class

  • Message Capture: Easily add messages to a list with the addMessage() method.
  • Sequential Messages: Each message is prefixed with a count to keep track of the order.
  • Assertion Printing: Use printAssert() to print all captured messages as an assertion, making it easy to locate issues in the debug log.

How to Use DebugUtil

Adding Messages:

DebugUtil.addMessage('This is a debug message');
DebugUtil.addMessage('Another debug message');

By using addMessage(), you can capture and store debug messages efficiently.

Printing Messages:

DebugUtil.printAssert();

When you need to print all captured messages, simply call printAssert(). This will output the messages in the debug log.

Best Practices

  • Use Meaningful Messages: Make sure your debug messages provide valuable information about the state of your application.
  • Limit Debugging in Production: Use the DebugUtil class primarily in development and testing environments to avoid performance issues in production.

Conclusion

The DebugUtil class is a versatile tool for enhancing your debugging process in Salesforce. By capturing and printing debug messages effectively, you can quickly identify and resolve issues, leading to more robust and reliable code. Start integrating DebugUtil into your workflow today and experience the benefits of organized and efficient debugging!

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply