Skip to content

Debug Logs in Salesforce

Salesforce Debug Logs

In the vast ecosystem of Salesforce, debugging and troubleshooting are critical processes. Whenever there’s a hitch or unexpected behavior in your application, debug logs come to the rescue. This article delves deep into Salesforce’s Debug Logging, illustrating its importance and guiding you on leveraging it for effective troubleshooting.

What is Debug Logging?

In Salesforce, a debug log is a detailed record of the operations that transpire during the execution of transactions, triggers, classes, or pages in your Salesforce environment. These logs offer a granular view of processes, including:

  • Database changes
  • HTTP callouts
  • Apex errors
  • Workflow processes

Setting Up Debug Logging

  1. Accessing Debug Logs: Go to Setup -> Under Logs, select Debug Logs.
  2. Creating New Trace Flags: Here, you define the criteria for what’s captured in logs. Click New Trace Flag, select a user, and define log levels.

Log Levels

Salesforce provides different log levels to determine the granularity of information captured. From finest to coarsest, they are:

  1. Finest: Captures maximum detail.
  2. Finer: Useful for detailed debugging.
  3. Fine: Fairly detailed information.
  4. Debug: Default level, includes essential information.
  5. Info: Provides necessary information.
  6. Warn: Only warnings are captured.
  7. Error: Only errors are captured.
  8. None: Disables logging.

Setting the right log level is crucial, as a high granularity level (like Finest) could quickly consume your available log space.

When to Use Debug Logging

  1. Apex Trigger and Class Issues: Debug logs can show the execution flow and values of variables at different stages, aiding in identifying logic flaws or errors.
  2. Visualforce Page Problems: If a Visualforce page isn’t behaving as expected, debug logs will showcase controller activities and other interactions to pinpoint the issue.
  3. API Calls: For integration with external systems, the logs can capture sent and received API messages, making it easier to identify any discrepancies.
  4. Workflow & Process Builder: When automated processes don’t fire or behave unexpectedly, logs can show criteria evaluations and actions.
  5. Unexpected System Behavior: If users report unanticipated behaviors, you can activate logging for their user profiles and reproduce the issue to study the log results.

Examples

Let’s explore a couple of scenarios to illustrate the power of debug logging:

1. Debugging a Trigger

Imagine a trigger designed to update the Account Name field on a Contact when it’s changed. However, it’s not working:

trigger UpdateAccountName on Contact (before insert, before update) {
    for(Contact c : Trigger.new) {
        if(c.AccountId != null && c.Account.Name != null) {
            c.LastName = c.Account.Name;  // this line is the culprit
        }
    }
}

By setting the log level to Finer or Finest, you’ll realize that you’re trying to access a related object field directly, which is not allowed in a trigger context. The debug log will throw a runtime error, allowing you to rectify the mistake.

2. Investigating Process Builder Issues

Suppose you have a Process Builder that’s supposed to send an email alert when an Opportunity is marked as Closed Won, but the email isn’t being sent. With debug logging activated for the user changing the Opportunity stage, you can trace the Process Builder’s criteria evaluation and see if it’s triggering the email action.

Tips and Best Practices

  1. Limited Storage: Salesforce retains only the last 20 debug logs per user, each log can’t exceed 2 MB, and logs are stored only for 7 days. Ensure that you retrieve and analyze logs promptly.
  2. Monitor Log Usage: Regularly monitor how much of your allowed log storage you’re consuming to avoid missing out on crucial logs.
  3. Optimize Log Levels: Use more detailed levels sparingly. Start with broader levels and drill down as needed to avoid clutter.
  4. Filter Logs: Use log filters to focus on specific events or operations, making troubleshooting more efficient.

Debug logging in Salesforce is an invaluable tool for developers, administrators, and consultants. By understanding its nuances and capabilities, you can swiftly identify, troubleshoot, and rectify issues in your Salesforce environment. Always remember to use logs judiciously and optimize their levels to get the most out of this powerful feature.

Case object optimizations in Salesforce that come out of the box

Tags:

Join the conversation

Your email address will not be published. Required fields are marked *

error: Content is protected !!