Log Flow Messages To Debug Logs : Luke Freeland

Log Flow Messages To Debug Logs
by: Luke Freeland
blow post content copied from  Blog
click here to view original post


A couple people recently have been asking for my help troubleshooting their flows. Their record-triggered flows worked great when ran through the flow builder’s “Debug” but not so when someone was saving records to the database. As a developer, my first thought is to enable debug logs. Debug Logs show some Flow info but many times it’s not detailed enough to see what values are at each element during execution. Let’s change that with a little code…

This post details how to use some Apex code invoked as an Apex Action to add additional diagnostic messages to your debug logs so you know what’s happening at run time.

Disclaimer: This should only be used in a sandbox and not included in your production flows.

High-Level Steps

  1. Create FlowDebugLogger Apex Class
  2. Add Debug Apex Actions to the Flow
  3. Enable Debug Logs
  4. Run The Flow
  5. View Debug Logs

Create FlowDebugLogger Apex Class

The FlowDebugLogger class is an Invocable Method that Flows can invoke using an Apex Action. It takes in a message that is text as input and outputs it into the debug log. To create it,

  1. Open the Developer Console as an Admin. Click the gear at the top right and then click “Developer Console”. This opens a new window.
  2. File –> New –> Apex Class.
  3. For the New Apex Class enter FlowDebugLogger and click Ok.
  4. Copy the code below into the editor replacing what’s there.
  5. File –> Save.
public with sharing class FlowDebugLogger {
    @InvocableMethod(label='Log Debug Message' description='Outputs messages to the debug log')
    public static void trace(List<String> debugMessages) {
        for (String debugMessage : debugMessages) {
            system.debug(debugMessage);
        }
    }
}

Add Debug Apex Actions to the Flow

Now that the FlowDebugLogger apex class is created, let’s add it to our Flows!

  1. Open a flow in Flow Builder.
  2. Add an Action to it.
  3. Type in Debug and Select “Log Debug Messsage”.
  4. Enter a Label and API Name. This can be really anything since this is temporary.
  5. Toggle debugMessages
  6. Enter a message that will be outputted to the debug log. This could be a variable, a message, or a formula that has a mixture of text and merge fields.
  7. Click Done.
  8. Repeat to add as many debug log messages as needed. Don’t forget to Activate your flow!

Note: This action can’t be the only element in the flow. If it is, the message doesn’t show up in the Flow!

In the Example above, the message “Hello World” was entered to be outputted to the Apex Debug Log.

Enable Debug Logs

Now the Debug Logs have to be enabled so log files are generated and we can see more information.

  1. Open Setup as a System Administrator then search for “Debug Logs” in the quick find textbox.
  2. Click Debug Logs.
  3. In the Monitored Users, click the New button.
  4. Click the magnifying glass and search for the user that will be running the flow.
  5. Enter the desired time span for how long the logs are enabled.
  6. For the Debug Level, ensure that the Apex Code is set to at least “Debug” and ensure Workflow is set to “Fine”.
  7. Click Save.

Run The Flow

Now run the flow. This can be done in the Flow Builder using the “Debug” button, through saving a record, and other ways depending on how the flow is invoked.

View Debug Logs

Now reopen the debug logs so the log files are now there. If the flow was ran via the Flow Builder’s “Debug” button, it’ll show up under Debug Logs as a /flow/<flow_name>/flow_interview_instance Operation. When ran from another way, it’ll be in one of the other operations. Click View on some of the logs to see the files. You may have to search more than one to find your execution.

...
19:52:10.230 (230738764)|METHOD_ENTRY|[1]|01p4W00000K97yd|FlowDebugLogger.FlowDebugLogger()
19:52:10.230 (230749872)|STATEMENT_EXECUTE|[1]
19:52:10.230 (230776829)|STATEMENT_EXECUTE|[1]
19:52:10.230 (230810892)|HEAP_ALLOCATE|[52]|Bytes:5
19:52:10.230 (230842435)|HEAP_ALLOCATE|[58]|Bytes:5
19:52:10.230 (230850442)|HEAP_ALLOCATE|[66]|Bytes:7
19:52:10.230 (230877941)|SYSTEM_MODE_ENTER|false
19:52:10.230 (230903068)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
19:52:10.230 (230912307)|SYSTEM_MODE_EXIT|false
19:52:10.230 (230942399)|METHOD_EXIT|[1]|FlowDebugLogger
19:52:10.230 (230970682)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
19:52:10.230 (230984546)|VARIABLE_SCOPE_BEGIN|[3]|debugMessages|List<String>|true|false
19:52:10.230 (231081049)|VARIABLE_ASSIGNMENT|[3]|debugMessages|["Hello World"]|0x769ea305
19:52:10.230 (231104447)|SYSTEM_MODE_ENTER|false
19:52:10.230 (231109967)|HEAP_ALLOCATE|[4]|Bytes:5
19:52:10.230 (231115909)|STATEMENT_EXECUTE|[3]
19:52:10.230 (231319879)|HEAP_ALLOCATE|[4]|Bytes:5
19:52:10.230 (231352682)|HEAP_ALLOCATE|[4]|Bytes:11
19:52:10.230 (231367005)|VARIABLE_SCOPE_BEGIN|[4]|debugMessage|String|false|false
19:52:10.230 (231389968)|VARIABLE_ASSIGNMENT|[4]|debugMessage|"Hello World"
...

Above is an excerpt of the debug log with the “Hello World” message in it from the Flow! This means the Apex Action ran.

Rerun the flow to generate a new debug log and view it again. Make as many tweaks as needed and repeat that cycle until you’ve figured out the issue. This may take hours or even a few days depending on the complexity of the flow(s) involved.

If you don’t see the output, it means that Apex Action didn’t run!

What other tips do you have for troubleshooting Flows? Let us know in the comments below!


April 27, 2021 at 05:41AM
Click here for more details...

=============================
The original post is available in Blog by Luke Freeland
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce