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 variablesSystem.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:
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!
Hello guys,
If you would like to view your debug logs live, check this tool:
http://www.debugbe.com
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.
Check out the Setup Audit Trail!
Thanks David
Thank you so much for this example!
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 = ‘
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
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?
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 ?
You’ll only be able to see their new logs after adding them – you won’t see their logs retroactively!
Hi David,
Could you tell me the basic difference between InteractionLog,DebugLog and SystemLog
I only know of one log, the DEBUG LOG!!!
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
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!
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?
Incoming! Although to be honest that’s the only feature most devs use!
Hey David
you know what i am also use system.debug when i want to troubleshoot my code.
it is very useful.
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?
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
LOL glad someone else enjoys that picture as much as I do! I am not insane!!
Hi David,
Do anyone can explain how the Query editor work?
Amith Rao
Hmmm you might be able to find a better explanation on another site!!