Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

Debug your code with System.debug!

February 22, 2014

Preface: this post is part of the Advanced Apex Concepts series.

Let’s do a little bit of role-play! No, not that kind of role-play! And definitely not this kind!

You – the perfect student!
You’re a tenacious learner and you’ve gone through all the chapters of Sfdc99 and their quizzes.
You’re motivated to learn Apex and you’ve even started attempting some triggers all on your own!

Me – the loving but not always helpful teacher!
My mom tells me I was more handsome when I was younger.

The problem: You’re writing some code and it doesn’t work. You don’t really know anyone who can help, and even though I’m a fine gentleman, my advice hasn’t been helpful. You’ve tried Googling for answers, pretending the inside of your brain is a computer – but no luck! What do you do now?

Good news! The answers to your problems exists, and it’s called System.debug!
System.debug(‘I am awesome!’);

System.debug() simply “prints” the value of anything you put inside it!

trigger CountFriends on Contact(before insert) {
  for (Contact c : Trigger.new) {
    // We often use System.debug to see the values of variables
System.debug('David has a new friend!'); System.debug('Hi ' + c.FirstName + ' ' + c.LastName + '!');
} List<Contact> total = [SELECT Id FROM Contact];
System.debug('David has ' + total.size() + ' friends total.');
}

To see the output of your System.debug, you need to:

  1. Write your trigger (no need for a test class yet!)
  2. Open up the Developer Console:
    – Click Your Name >> Developer Console on the top right of any Salesforce page
    – You must have the “View All Data” permission to access this!
  3. Do something in Salesforce that will make your trigger run!
  4. Open the log for your latest action, then filter to show “Debug Only”

Here’s what the above steps will look like with our example:

How exactly does System.debug help you troubleshoot your code?
Simple! At the very minimum, System.debug will tell you where your code is breaking. For example, you can track the value of a variable with multiple System.debugs and see where it’s no longer what you expect. Most of the time, knowing where your code breaks is enough to show you how to fix it. If not, you now have a tangible reference point to begin your Google search for a solution!

Don’t be afraid to use System.debug often in your triggers!

Next post: Comparing old and new values in a trigger!

22 Comments
Stephen Logovin
November 26, 2022 @ 1:34 pm

Hello guys,

If you would like to view your debug logs live, check this tool:

http://www.debugbe.com

Reply
Learner
August 15, 2017 @ 6:34 am

If I want to see the log of regular trigger, how can I do that? Like when I assign public group or permission set, I need to understand what object is involved. I added begug log for that user. But the log only shows .apexp page name and execution started and finished. I assume we cant see this in developer console log too. So how to know what objects are involved in this. Is there any other way? Thanks a lot in advance David.

Reply
    David Liu
    August 15, 2017 @ 9:36 pm

    Check out the Setup Audit Trail!

    Reply
      Learner
      August 16, 2017 @ 8:56 pm

      Thanks David

      Reply
      Anonymous
      February 24, 2022 @ 9:15 pm

      Thank you so much for this example!

      Reply
Ed
May 10, 2017 @ 12:41 pm

I execute the following in the anonymous execution: Database.executeBatch(new TransferACDCAccountOppOwnerBatch(), 200);
The class is below. It uses Batchable interface. I put in a bunch of debug statements but don’t see anything in the logs in the console. I also create debug logs at Debug level for Apex, etc. and also don’t see any of the system.debug output. The SOQL itself I know works and should only return one row in the developer sandbox test data I setup.
Is there something about the batchable interface that doesn’t allow system.debug output? I know it’s asynchronous … but the job completes and I see plenty of log information … I just don’t see any system.debug output.
global class TransferACDCAccountOppOwnerBatch implements Database.Batchable {
String query;
static String type = ‘Accountant’;
static String stageName = ‘Closed Won’;
static String numberEmployees = ‘

Reply
Mudassar
March 7, 2017 @ 9:43 pm

i am unable to debug my code r check the parameter values or query result :( :(
what should i do i dont want to leave coding in apex

Reply
Ella
April 30, 2016 @ 11:17 pm

I’m trying to retrieve email headers from an inbound email and then assign it to a custom field. Is that possible? Is there a way for use to do this using system debug?

Reply
Jakson Monteiro
January 28, 2015 @ 10:13 pm

Hi David,

Note we can also check Debug log from the Monitor>>Logs>>Debug Logs.

but sometimes i am not able to check or see the logs even after adding Monitored user ?

Reply
    David Liu
    January 28, 2015 @ 10:18 pm

    You’ll only be able to see their new logs after adding them – you won’t see their logs retroactively!

    Reply
Neenu Mathew
August 26, 2014 @ 10:14 pm

Hi David,
Could you tell me the basic difference between InteractionLog,DebugLog and SystemLog

Reply
    David Liu
    August 27, 2014 @ 10:53 pm

    I only know of one log, the DEBUG LOG!!!

    Reply
Neenu Mathew
August 21, 2014 @ 4:29 am

Hi David,

One question.While checking debug log how can we recogonize which one is the one we want to test.To be clear
If I have an apex test class and I have given “RunTest” now while checking in the debug log we can see a lot of logs coming up.Of these how can I identify this is a test class log.Hope my question is clear.Please help me to figure out

Reply
    David Liu
    August 21, 2014 @ 8:57 pm

    Hhmmm you’ll usually notice a certain naming pattern of the ones that are triggered by your code – this will come over time! For now just use System.debug() and you’ll know exactly which one is which!

    Reply
Tom B
August 16, 2014 @ 4:44 am

Great post as always David! Can you create a part two and get more detailed on how we can make use of the other information that is generated in a log?

Reply
    David Liu
    August 16, 2014 @ 3:52 pm

    Incoming! Although to be honest that’s the only feature most devs use!

    Reply
Viru
June 11, 2014 @ 4:06 am

Hey David

you know what i am also use system.debug when i want to troubleshoot my code.
it is very useful.

Reply
    Tom B
    August 16, 2014 @ 4:42 am

    Great post as always David! Can you create a part two and get more detailed on how we can make use of the other information that is generated in a log?

    Reply
Amit
April 1, 2014 @ 3:36 am

Hi David,

Ha ha ha…. :-D

Let’s do a little bit of role-play! No, not that kind of role-play! And definitely not this kind!
In above line, On ‘this kind’ link, you have set very ridiculous image to explain role… :-)
I am still getting laugh whenever i remember this image… You are awesome David for choosing accurate images to explain your point…

Thank you very much,
Amit

Reply
    David Liu
    April 1, 2014 @ 8:05 am

    LOL glad someone else enjoys that picture as much as I do! I am not insane!!

    Reply
    Amith Rao
    August 13, 2015 @ 6:04 am

    Hi David,

    Do anyone can explain how the Query editor work?
    Amith Rao

    Reply
      David Liu
      August 13, 2015 @ 8:11 am

      Hmmm you might be able to find a better explanation on another site!!

      Reply

Leave a Reply Cancel reply

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


*

*

Theme: Simple Style by Fimply