Debugging Your Code With System.Debug()


Debugging code is also an integral part of the development process. Through debugging, we can identify where our Apex code is breaking and can correct it accordingly. Read more about Debugging here.

What is System.debug()?

The system is a class, and debug is a method of the System class.

System.debug() is used for debugging our Apex code in Salesforce. With the help of this method, we can print any values in Apex code.

It mainly helps us to debug the errors we encounter in our code.

After writing our code having System.debug(), we run it. So before executing our code, check the ’Open Log’ checkbox, and then we need to click on the ‘Debug Only’ checkbox to see the output of this method.

Debugging Your Code With System.debug()

public class Demo


public void example()


map doctorslist = new map([select name, doctor_email__c, monthly_salary__c from doctor__c]);

for(id k: doctorslist.keyset())


system.debug('Record id: ' + k);

system.debug('Name: ' + doctorslist.get(k).name);





In the above scenario, we have a class named ‘Demo,’ and we have a method ‘example’. If we look into the method, there is a Map of Id as Key and Doctor record as Value, and we are fetching mentioned field values of all the doctor records present into the org.

Now, we are printing the Record Id and Name of the doctors by iterating over Map.

So, here System.debug() has helped to print the Doctor Id and Name in our Apex code.

To execute this code, open Anonymous Window and write the below code,

Demo d = new Demo();


Mark the ‘Open Log’ checkbox checked and click on Execute.

Then click on ‘DEBUG ONLY’ to see the output.


public class Demo2{

public void listExample(){

List names = new List();





for(Integer i = 0; i < names.size(); i++){







  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.