Test and Debug Record Triggered Flows in Salesforce

In this post we will use a simple hack to Test and Debug Record Triggered Flows in Salesforce. This debugging hack will work for After Insert Flows, After Update Flows and Before Delete Record Triggered Flows. We will see how to easily get Debug Logs or Flow Execution Logs while Testing Record Triggered Flows.

Test and Debug Flows

Why and When Debug Logs / Flow Logs are needed for Record Triggered Flows?

Do you miss getting the detailed Logs with element by element execution details while working on Record Triggered Flows? Specially when the Flow has multiple Elements and complex business logic. 
For example, below is a complex Record Triggered Flow with many elements. If something goes wrong in this or if flow is not giving expected result and also not throwing any error or exception, it's not easy to debug without having Flow Execution Path details as we get for other Flows on UI.
Test and Debug Flows
Since we cannot Test and Debug Record Triggered Flows from UI directly using the native Flow debug tool, it is sometimes very difficult to understand the root cause or find an issue if Flow is not behaving as expected. In such scenarios we often want to see the Element by Element Flow execution path to determine if the Flow is behaving working as expected.

Unfortunately, in case of Record Triggered Flows, Flow debug/execution log is sent to Flow owner/developer only if there is an Exception or Error while executing/running the Flow. 
But in many scenarios Exception is not received and Flow still behaves weird and gives unexpected results. This is mostly because of some logic mistake in Flow or some other development issues in Flow. Without detailed Flow logs, we are many times not able to easily check and identify how the Flow is executing and which elements it is traversing and where the issue might be specially in complex Record Triggered Flows.

Workaround : One approach to solve this problem while working on Record Triggered Flows is by Forcing an Exception or Error when the Flow executes. For example, by adding a condition or update in Flow which fires a validation rule and breaks the transaction and throws an error/exception during run time. By doing this, a Flow Exception email is received by flow owner with complete Flow Execution path and element by element detail.


Note: Do check Process Automation Settings from Setup to see if the Flow Error Emails are received by Flow Owner or not. There are two options for Flow Errors Emails, one to the flow owner or one to apex exception recipients. To receive the email, you should make sure the setting is as expected.

An Example of Flow Logs about which I am talking about:

Test and Debug Record Triggered Flows

Well in this article I am going to show a simple solution to test record triggered flows and get debug logs in email without thinking much about how to generate or force an error in any Flow. 

We will use apex code for this. Don't worry if you are not a developer because you can simply copy paste the code or directly install the package and use in Flows.

Note: This will not work for Before Insert or Before Update Flows.

How to Test and Debug Record Triggered Flows?

When we build any Record Triggered Flow, we can simply add an Apex Flow Action which I will be providing the link to in this article later. This apex action creates an exception and Flow owner receives an email with Flow Execution Logs.

Learn Salesforce Flows
Become Flow Champion - Click Image and Start Learning

How will this Work and Help?

As a part of Flow testing, this apex action will create a custom and forced exception which will make the Flow to fail. Because of this the Flow Owner will get an email with the detailed Flow execution log as per the screenshot I posted above in the article. 

Note: The best part is that this is a Re-usable Action, so you can add this to any Record Triggered Flow except before update/insert ones as they don't allow apex actions yet 
This action can be used for quickly creating a test scenario and test your Record Triggered Flows to get Flow Execution Logs in Email.

Steps to use this in Record Triggered Flows

1. Install the Package in Sandbox or Developer Org from the Link below.

Link for Dev org : Install in Dev Org
Link for Sandbox: Install in Sandbox

Debug Record Triggered Flows


2. Add the Apex Flow action at the last in Flow. The Action name from my Package is Debug Flow. Just add an Action Element on the Flow and Search for Debug Flow and Select this Action as installed from package. Provide a Label and just save it. Save as or Save and Activate the Flow before testing.

Debug Flows Salesforce
Check video below for Steps.


3. Test and debug the Flow by doing record insert/update/delete in dev or sandbox as per the type of flow being tested. For example if the Flow you need to test is an After Insert Trigger Flow on Case, simply create a new Case to test. Make sure you match the conditions required to fire this Flow.

4. Flow will run but will fail eventually because of our apex action and the Flow owner will receive the Email with Flow execution details. This is where you can check flow execution path and see for any weird or unwanted behaviour.
Example of the Log
How to Debug Record Triggered Flow

5. After testing is complete and verified remove the action from the Flow.

Update Spring' 21 Release : Now when we receive Flow Error Email, the error Email will have a link to the Flow and it will clearly show where the Flow Failed on the Flow Canvas with Error Path and Element. Easier Debugging!! :)

Do checkout all Spring' 21 Flow Features :  Spring 21 Flow Features
 
Best Practises/Tips for using this approach in Record Triggered Flows

1. Never test in Production.Use this in Sandbox only. As we should not ever test Flows in Production this approach or any debug of Flows should be done in Sandbox only. Specially the Record Triggered Flows.
2. Do this to validate Flow Logic and understand Flow Execution before deploying the Flows and remove this once testing is complete and flow logic works as expected.
3. Ideally add Fault Path/Connectors in Flow Elements once the Flow logic Testing is done and verified and Flow works as expected. This is because to know if there are any issues in Flow Logic or Execution.
4. Remove after debugging and testing.

For your reference, below is the Sample Code of the Apex Action in case the Package is having installation issues. The Apex Action Name generated from below code is FlowDebug. Just copy paste all three classes and Apex Action is ready to be used in Flows. 
This is a very basic example and use case. You can enhance or modify this if needed as per your use case.

1. Apex Class 


global class CustomExceptions {

    public class RequiredException extends Exception {}

}


2. Apex Class


global class FlowDebug {
   
    @InvocableMethod
    public static list<string> getAccountIds(list<string> check) {    
        throw new CustomExceptions.RequiredException('This is a dummy Exception to Debug Flow. Check Email for Flow Log');
   }
}


3. Test Class


@isTest 
private class FlowDebugTestClass {

    static testmethod void testFlowDebug(){
        CustomExceptions cx= new CustomExceptions();
        CustomExceptions.RequiredException re = new CustomExceptions.RequiredException();
        List<String> dummyLst= new List<String>();
        dummyLst.add('AccidentalCoderSf.com');
        
        try{
            FlowDebug.getAccountIds(dummyLst);
        }
        catch(exception e){
        }
    }
}

Thanks for reading! Hope this helps.

Please comment or suggest if you have nay other ways or ideas around this topic.

CHECKOUT ALL FLOW EXAMPLES ON THIS LINK :  FLOW EXAMPLES AND USE CASES: LEARN HOW TO BUILD FLOWS

FLOWS ARE GETTING MORE POWERS IN SPRING '21 RELEASE. CHECK LATEST NEW FLOW FEATURES IN UPCOMING SALESFORCE SPRING 21 RELEASE  https://www.accidentalcodersf.com/2020/12/salesforce-spring-21-release-latest-flow-features.html

If you like reading my content, Do Subscribe to my Upcoming YOUTUBE Channel to receive latest updates here: https://www.youtube.com/channel/UCdfi8Sq7iOcxnFhsgulWHeg/videos?view=0&sort=p&flow=grid

Join 400+ Subscribers and receive all my latest Posts directly in your Inbox.

Enter your email address:

Delivered by FeedBurner


Comments

  1. Frederick Mark Riley11 December 2020 at 16:53

    Hi There. I was relieved to find this workaround for such an obvious requirement. However I am not receiving any emails with the flow log. ?
    Regards
    mark.riley@xenogenix.co.uk

    ReplyDelete
    Replies
    1. Plz go to Process automation settings from Setup and set the last option Send Process or Flow Error Email to User Who Last Modified the Process or Flow. Also check your Email Deliverability settings .. Let me know if you still face issues

      Delete
  2. Hello, I followed the install process, validated that my process automation settings are correct (I've received other flow error emails), ensured that my flow is activated and checked junk mail. I'm not receiving them email. Can you think of anything else to check that I've missed?

    ReplyDelete

Post a comment

Thanks for your Feedback!

POPULAR POSTS

Salesforce Flow Examples : Flow Use Cases and Scenarios

Salesforce Spring '21 Release Preview | Latest Flow Features

Spring '21 Flows | Is Changed in Record Triggered Flows using Prior Value


Never Miss Latest Posts