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.

We need to login to Salesforce first before navigating to Developer Console

Breakpoint supported in case of interactive debugging

Checkpoint is supported

Only Raw log is available with all detailed information

Raw log as well as other utilities like filtering are supported

It needs to be installed on desktop

Browser based

It does not has as many as features compared to Developer console

Developer 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

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”.

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”.

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).

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

Developer console shortcuts

Function

Key

Open

CTRL+O

Open Lightning Resources

CTRL+SHIFT+A

Open log

CTRL+G

Open raw log

CTRL+SHIFT+G

Download log

CTRL+ALT+G

Save the current view

CTRL+S

Save all

CTRL+SHIFT+S

Delete

CTRL+DELETE

Close

CTRL+/

Close all

CTRL+ALT+/

Open the Execute Anonymous window

CTRL+E

Execute anonymous Apex code when the Execute Anonymous window is open or the last executed code when the window is closed

CTRL+ALT+E

View log panels...

CTRL+P

Log panel

SHIFT+ALT+G

Show/Hide help for the shortcut key

CTRL+SHIFT+?

Find

CTRL+F

Find/replace

CTRL+SHIFT+F

Find/replace all

CTRL+SHIFT+R

Search in files

CTRL+SHIFT+H

Fix indentation

SHIFT+TAB

Open resource

CTRL+SHIFT+O

Clear log panel

SHIFT+ALT+G

Show Preview of the Visualforce page/component

CTRL+J

Navigate to the declaration of a selected object in the Source Code Editor

CTRL+ALT+N

Show/Hide the Command Line Console

CTRL+SHIFT+L

Navigate backward through the view history

CTRL+,

Navigate forward through the view history

CTRL+.

Navigate backward through the open views

CTRL+PAGE UP

Navigate forward through the open views

CTRL+PAGE DOWN

Toggle full screen editing of the current view, if available

F11

Exit full screen editing of the current view, if active

ESC

Video : Interactive Apex debugging

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