Advance Apex debugging in Salesforce and best practices – Videos

How to resolve apex debug log size limit issue, Difference between Eclipse and developer console, Interactive Apex Debugging, Keyboard shortcuts for Developer console and Other best practices

While developing applications on Force.com platform using Apex, I am sure you may have been in need of debugging your code. Salesforce being cloud platform, method to debug code is very different as compared to other programming languages. In this post we will discuss all about Apex code debugging, challenges, solutions and other best practices.

Salesforce Advance Apex debugging
Salesforce Advance Apex debugging

How many ways to debug Apex code

  1. Eclipse Force.com IDE
  2. Debug log
  3. Developer console
  4. Fetch debug log using Tooling API


Eclipse IDE vs developer console

EclipseDeveloper Console
It can support Multiple Projects in Same workspaceWe need to login to Salesforce first before navigating to Developer Console
Breakpoint supported in case of interactive debuggingCheckpoint is supported
Only Raw log is available with all detailed informationRaw log as well as other utilities like filtering are supported
It needs to be installed on desktopBrowser based
It does not has as many as features compared to Developer consoleDeveloper console is future and with each release its growing. Currently performance analysis, Query execution plan , Save sequence and many more functionality supported

How to overcome with debug log size limit issue

It is possible that you are trying to debug an application however you are hitting limit for debug log size. By default debug log size is 2MB, This 2MB would be last portion of file. Check if you have something like “Skipped xx bytes of detailed log” in the very start of your log, If you find this statement that means debug log is truncated and it does not has all information.

It can be resolved by following below steps

  1. Change debug log filters to only show intended Category. In my most of cases I choose only “Apex code” with Level “Debug” and all other category “None”.
  2. If this still doesn’t solves your problem and debug log is more than 2MB then as a best practice, instead of using System.debug(‘msg’) , use System.debug(LogLevel.ERROR, ‘msg’) and set Level for “Apex Code” as “Error”.
  3. Chances are very high that you are able to get your debug logs from Apex code. If you still have issue then set Log level for everything including Apex as “None” and navigate to Apex class you want to debug and set log level explicitly for this class. Other way to set log level for individual class is by navigating to developer console, “Debug | Change log level | Class and Trigger Trace Overrides | Add” and select you Apex class or trigger (as shown in below image).
Salesforce - Setting log level for Apex class or trigger in Developer Console
Salesforce – Setting log level for Apex class or trigger in Developer Console

Breakpoints (Interactive Apex Debugging)

In case of breakpoint, execution of program stops at specified point and debugging can be resumed line by line providing complete information on variables and other state of code at that time.

  • It is paid
  • Only one debugging session per Org
    • Believe me, its very very very complex to debug cloud application however Salesforce is at least able to provide worlds first multi tenant interactive debugger in initial form
  • Only Sandboxes are supported
  • Only used in Apex code and Triggers
  • Managed code / Variable information not available
  • Not supported in Developer Console
  • Currently only Eclipse supports
  • It is based on DBGP (Debug protocol) so that any Salesforce IDE should be able to debug it

More information on interactive debugging in video available at bottom of this article

Checkpoint

Using checkpoint, we can gather values in variables at that particular line execution. Below are some important information about checkpoint :

  • Similar to breakpoint but it does not halt execution at that point
  • Checkpoint can be viewed in in two tabs of perspective
    • Checkpoint inspector
    • Checkpoints tab
  • Checkpoint statistics gathered right before the line of code with checkpoint executed
    • Statistics are gathered only once, regardless of how many times code is executed
  • We can run SOQL or apex code at that checkpoint

Perspective

Debug log can be opened in many perspective view like All, Debug, Analysis and Log Only. Select log entry in Logs panel and navigate to “Debug | Switch Perspective” and select you favorite perspective, by default “Debug Only” perspective is selected.

Following are different panels available in Developer console Perspective (as shown in below image)

  • Stack Trace
    • Execution Tree
    • Performance Tree
  • Execution Log
  • Execution Stack
  • Execution Overview – It is very important panel to get idea of sequence of Save order with colored chart , debug performance issue and get idea of limits used by code
    • Save Order
      • It shows sequence of execution of different components like Trigger, Workflow, and Validations etc. Red and Orange color of trigger indicates before and after trigger
    • Limits
      • To see data in this tab, profile should be Finest
    • Timeline
      • This panel shows how much time spent (milliseconds and percentage) in components like Apex, Workflow, DB etc
    • Executed Units
      • It shows name of all components executed like Validation, Trigger, Workflow etc

Optionally below panels can also be selected by pressing “Ctrl+P”

  • Source
    • Shows source code or metadata for the elements on the stack during current transaction for line selected on debug log
  • Variable
    • Shows when variable has been assigned and its value for line selected on debug log
Salesforce Developer console perspective
Salesforce Developer console perspective

Developer console shortcuts

FunctionKey
OpenCTRL+O
Open Lightning ResourcesCTRL+SHIFT+A
Open logCTRL+G
Open raw logCTRL+SHIFT+G
Download logCTRL+ALT+G
Save the current viewCTRL+S
Save allCTRL+SHIFT+S
DeleteCTRL+DELETE
CloseCTRL+/
Close allCTRL+ALT+/
Open the Execute Anonymous windowCTRL+E
Execute anonymous Apex code when the Execute Anonymous window is open or the last executed code when the window is closedCTRL+ALT+E
View log panels...CTRL+P
Log panelSHIFT+ALT+G
Show/Hide help for the shortcut keyCTRL+SHIFT+?
FindCTRL+F
Find/replaceCTRL+SHIFT+F
Find/replace allCTRL+SHIFT+R
Search in filesCTRL+SHIFT+H
Fix indentationSHIFT+TAB
Open resourceCTRL+SHIFT+O
Clear log panelSHIFT+ALT+G
Show Preview of the Visualforce page/componentCTRL+J
Navigate to the declaration of a selected object in the Source Code EditorCTRL+ALT+N
Show/Hide the Command Line ConsoleCTRL+SHIFT+L
Navigate backward through the view historyCTRL+,
Navigate forward through the view historyCTRL+.
Navigate backward through the open viewsCTRL+PAGE UP
Navigate forward through the open viewsCTRL+PAGE DOWN
Toggle full screen editing of the current view, if availableF11
Exit full screen editing of the current view, if activeESC

Video : Interactive Apex debugging

Video : A Deep Dive into Debugging Applications on the Salesforce1 Platform

Related posts

  • Luis Cosmes

    Great post, is interactive debugging available in beta already? If not, when would it be expected to be in beta?

  • Ankush Somani

    How to enable breakpoint debugging or debug log in eclispe. any external jar file we need to include for this ?

    • Hi @ankushsomani09:disqus we need to contact Salesforce to enable it. It is actually paid feature.

  • Pingback: An Example Force.com Development Workflow - Daniel Eagle()