Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 3.79 KB

Logging-&-Debugging.md

File metadata and controls

64 lines (47 loc) · 3.79 KB

Logging & Debugging

Debugging

Unlike traditional languages such as Java, C++, and C#, Apex and Visualforce can be quite cumbersome and time consuming when trying to debug. You don’t have the luxury of being able to step through the code during runtime.

Instead of having the ability to set breakpoints and halt execution of the code, you must instead use logging to capture the flow of events to help you troubleshoot the code.

Salesforce includes a debug log that allows you to output strings or even deserialized objects to help you understand what the value of specific complex variables were at specific times during runtime execution.

Take a look at the example below. Take note of the line that includes System.Debug(…);. When executed, this statement will write a log entry to the Salesforce debug log that you can later view by going to the Developer Console via the Salesforce UI.

Public with sharing class SampleClass
{
  public List<Account> getLeads(Date CreatedDate)
  {
    List<Lead> = leads = [SELECT id,FirstName,LastName FROM Lead WHERE CreatedDate =: CreatedDate];
    System.Debug('Leads Retrieved: ' + leads);
  }
}

While writing Apex code you should always make sure to add debug statements in key areas of your code that would help in debugging should errors occur.

Take a look at the example below that expands our example above.

Public with sharing class SampleClass
{
  public List<Account> getLeads(Date CreatedDate)
  {
    List<Lead> = leads = [SELECT id,FirstName,LastName FROM Lead WHERE CreatedDate =: CreatedDate];
    System.Debug('Leads Retrieved: ' + leads);
  }

  public List<Account> updateLeads(List<Lead> leads)
  {
    try
    {
      insert leads;
    }
    catch(DMLException e)
    {
      System.Debug('An error occurred while inserting leads. Exception: ' + e.getLineNumber() + ':' + getStackTraceString);
    }
  }
} 

Notice in the code above that we have added a new method to update our lead records and have added a try/catch block to catch an exception that has occurred.

Adding debug statements within the catch block is an excellent way to help troubleshoot issues in the future.

Custom Logging

The Salesforce debug log is good way to debug your code when you have a limited amount of logs that you want to review over a short period of time. When the Salesforce debug log breaks down is when you want to review logs for issues that occurred the previous day or before. Additionally, if you wish to receive a notification of when an issue occurred this is also outside the scope of functionality of the Salesforce debug log.

It is for use cases like these that you want to use a custom logging framework in addition to the debug log to adequately troubleshoot your code.

The logging framework should write log entries to a custom object. From there you can create workflow rules to send email notifications, create tasks, or even create a ticket in an external system when an error event occurs.

Although this is a very helpful approach to logging and error notification in Salesforce you must take care when using it. Unlike using the debug log you must write records to the logging object for this approach, which takes up DML calls (Apex's Data Manipulation Language) that may be needed for your application. Keep in mind your DML limits at all times when using this approach. Also keep in mind that every record created will also take up storage in your org so you will need to develop an approach for deleting log records that are no longer needed.

Make sure that when using this custom logging approach, you write a mechanism that will allow you to set the logging level and turn on/off logging by user and/or profile using custom settings by application. This will help you to avoid creating thousands of logging records unnecessarily taking up storage limits.