Friday, November 4, 2011

Salesforce How to Debug / Troubleshoot using the Salesforce Debug Logs

Setting up your user for Debug Log monitoring
  1. Name / Setup / Administration Setup / Monitoring / Debug Logs
  2. Click ‘New’.
  3. Select your User from the lookup. Click 'Save'.
    1. Your user is now setup to monitor and record a debug log for every page load of a visualforce page in salesforce. These logs can be viewed or downloaded for review.
    2. You get 20 Debug Log requests at a time... this is to prevent stacking up a bunch of unused logs, so whenever you are done using the debug logs, remove your User from the list to monitor debug logs, or click 'Reset' next to your User and it will give you a fresh 20 requests. Otherwise your name will be automatically removed from the list to monitor debug logs once you reach your 20 request limit.
Using the Debug Log



  1. Scenario: You are working on a Visualforce page and running into an error in a certain method, there is an 'IF' block that should be firing that you are not sure why it is not. You can add this line of code where appropriate to view each line of code as it is fired and in what order... "System.Debug('String');" and that 'String' you entered in will be its own individual line in the Debug Log every time that page is loaded. See Example below:




















  1. So in this method, 'checkForErrorsOnPage()', I have added a System.Debug('String'); in serveral places so I can monitor each step of the process to see which elements in the code are being executed and in what order.

    Below is the corresponding debug log for these System.Debug() statements. By using 'ctrl + F' search function in the best browser available, Google Chrome, you can search for the keyword, 'Bazinga' and find your Debug statements.








    So from looking at the Debug Log, I see that 'Bazinga.... 2' was never fired, and I can then continue troubleshooting around why that line was not hit.


    Other Userful Debug Messages


    You can output variable values as well instead of just using a string message, this can be helpful for checking a list size at a certain point, or a boolean value at anotyher point in your code. See examples.

    List Size Output: System.Debug('someList's size at this point is: ' + someList.size());

    Boolean Value Output: System.Debug('someBoolean's value at this point is: ' + someBoolean);

No comments:

Post a Comment