Apex Class

How to Debug an Apex Class in Salesforce

Sometimes you have to deal with errors on your Apex code but you do not know what to do to find them so here we will provide you a few options that you can take to debug an Apex Class with examples.

Debug an Apex Class in Salesforce

Debug Logs

Salesforce provided us a statement called System.debug() in which you can display the current value of a variable on the Developer Console at the exact time on the code when you added that statement.

This is quite useful in some scenarios when you want to better understand what the code does or determine if the code is reaching a specific part of the code.

This is an example of an Apex Class which populates a text field based on the contact's age.

public class ContactDetails {

    public static void isMemberAdult(Id contId) {       
        Contact con = [SELECT Id, Age__c, AgeCategory__c FROM Contact WHERE Id =: contId];
        
        if (con.Age__c >= 18) {
			con.AgeCategory__c = 'Adult';
        }else if(con.Age__c < 18){
            con.AgeCategory__c = 'Minor';
        }
        update con;
    }
}

However, it's not working fine since the Contact that I'm using is still in null, here's how you can debug it:

public class ContactDetails {

    public static void isMemberAdult(Id contId) {       
        Contact con = [SELECT Id, Age__c, AgeCategory__c FROM Contact WHERE Id =: contId];
        
        System.debug(con.Age__c);
        System.debug(con.Age__c >= 18);
        System.debug(con.Age__c < 18);
        
        if (con.Age__c >= 18) {
			con.AgeCategory__c = 'Adult';
        }else if(con.Age__c < 18){
            con.AgeCategory__c = 'Minor';
        }
        update con;
    }
}

To see those System.debug() you need to open the Developer Console, go to Logs, find the log that you recently executed (probably the last one), then click on the checkbox Debug Only above the Logs tab.

Debugs logs on the Developer Console in Salesforce

As you can see in the debug logs, the Contact wasn't working since the Age__c field was null. This is one of the fundamental and easier ways to discover an error on an Apex Class.

Apex Test Execution

Creating an Apex test Class is mandatory to deploying your Apex Classes into production, but it's for an important reason. It helps you not only to discover new errors in your code but also to create more maintainable Apex since if in the future you modify the Apex, with the test class you can verify that the logic keeps working as expected.

In our case to debug Apex classes, it could be really useful to use the Assert.AreEquals statements. This is how you could debug the previous error using an Apex Class

@isTest
public class ContactDetailsTest {
	
	@isTest    
    public static void isMemberAdultTest(){
        Contact cont = new Contact();
        cont.Firstname = 'John';
		cont.Lastname = 'Doe';
        // AGE IS NULL
        insert cont;
        
        ContactDetails.isMemberAdult(cont.Id);
        Assert.areEqual('Adult', cont.AgeCategory__c);
    }
}

Once you execute that test class, on the debug log, the Assert method will provide you both actual and expected values. In our case, the test class failed because the Contact didn't have an Age, the same as before.

How to debug an Apex Method: Assert.areEquals failing on a Salesforce Apex Test Class

Trace Flags

Imagine that your Apex Class is working fine with your System Administrator user but it fails for a user with fewer permissions than you, and it doesn't have access to the Developer Console to inspect the debug logs, what could you do?

Basically, you can go to Setup > Debug Logs > Click Edit, next to the User and there you need to set a Start Date and an Expiration Date in which their debugs will be appearing on "Debugs Logs". Once you configured this you can see the debugs from another user on the organization, this will help you

Debugs logs individually for each user on Salesforce