LinkedIn

Screen Flows - Exception Handling & Logging - Custom Platform Events Vs Flow Execution Error Event

In the previous post, we saw how to handle and log exceptions in auto-launched flows. In this post let us see Exception handling and logging for screen flows.

For this let us use the below Use case:

Object - Account

Flow Functionality - If the Account type(picklist with values End-User, Partner) is Partner Account – Display a screen to enter additional details by user and based on entered values, decide Account Partnership level(picklist) as Diamond, Gold or Silver. This screen flow will be added to Account record page. Below is the Screen flow which implements this.


Scenario - 1 - Partnership Level Updated Successfully

Add this flow to Account Record page like shown below:



We can make this conditionally visible on the record page by adding component visibility criteria so that screen will be visible only if Account Type is Partner:


Now let us create an Account  - Account ABC with below details:

Name - Account ABC

Active - True

Account Type - Partner

And we can see that, flow component is visible on the screen, and select values from the screen as shown below:


Transaction Behavior

You can see that Partner Level got updated as Diamond

Scenario 2 - Add Validation rule to fail Account Save

Let us add below validation rule in account so that flow update is going to fail.


Transaction Behavior

Execute the flow and you will see an error message on the screen as shown below



Scenario 3 - Add fault Connector and publish Platform event

Like we have done in the previous post of autolaunched flow exception handling, here also let us add a fault connector to publish a platform event and also try to add a custom message to show on the screen instead of standard error message.

Platform event


Publish Platform Event Action in Flow as shown below:


Add User Friendly Error Message Screen


Flow after Adding publish Platform Event and custom error screen:

We have a trigger on Platform event already which process platform event and create Exception record:



Transaction Behavior

You can see custom error message on screen and also an exception record got created:


Exception Record:


Scenario 4 - Log Exception using Flow Execution Error Event

Whenever an exception is happening within a screen flow, Salesforce automatically publish an event to FlowExecutionErrorEvent - which is a standard event. So we can subscribe to this event and log details to Exception record.

As per Salesforce documentetion  FlowExecutionErrorEvent can be subscribed in below mentioned methods:

But Apex Triggers was also supported before and I was able to create a trigger on FlowExecutionErrorEvent, before it was getting disabled:



Or you can also use a process builder to subscribe to FlowExecutionErrorEvent as shown below:




Transaction Behavior

Since we are not handling exception, user will be seeing standard error message on the screen


But Exception details will be logged using PB/Trigger


Comparison between Custom Platform event and FlowExecutionErrorEvent approach for Exception Logging



So we have more flexibility on exception logging if we use custom Platform event. Also I have noticed that FlowExecutionErrorEvent.ContextRecordId is not getting populated like expected in most of the cases.

As of now FlowExecutionErrorEvent is only supported for Screenflows. If that is available for autoaunched flows also, probably then it can be considered as suitable option for exception logging since it is not consuming any governor limits and we don't need custom platform event.

Below is a demo of both approaches:



Resources:


Updating Apex code used below:

Trigger on Platform Event to Create Exception record:

trigger processExceptionPE on Exception_Log__e (after insert) {
    List<Exception__c> ecelist = new List<Exception__c>();
    for(Exception_Log__e ex : Trigger.new){
        Exception__c excep = new Exception__c();
        excep.Object__c = ex.Object__c;
        excep.Operation__c=ex.Operation__c;
        excep.Record_ID__c=ex.Record_ID__c;
        excep.Exception_Details__c =  ex.Exception_Details__c;
        ecelist.add(excep);
    }
    
    if(ecelist.size()>0){
        insert ecelist;
    }
}

Test Class
/************************
* Test class for processExceptionPE
* **********************/
@isTest
public class processExceptionPETest {
    @isTest static void testprocessPE(){
        Test.startTest();  
        Exception_Log__e pe = new Exception_Log__e();
        pe.Object__c = 'Account';
        pe.Operation__c = 'Account Country Population';
        pe.Exception_Details__c = 'Exception happened during process';
        pe.Record_ID__c = 'accid';
        eventBus.publish(pe);
        Test.stopTest();
        List<Exception__c> ex = [SELECT id from Exception__c where Object__c='Account'];
        System.assert(ex.size()>0);
    }
    
}
Trigger On Flow ExecutionErrorEvent to create Exception record:

trigger triggeronflowerror on FlowExecutionErrorEvent (after insert) {
    
    List<Exception__c> ecelist = new List<Exception__c>();
    for(FlowExecutionErrorEvent ex : Trigger.new){
        Exception__c excep = new Exception__c();
        excep.Object__c = ex.ContextObject;
        excep.Operation__c='Populate Account Partnership Level from Floww trigger';
        excep.Record_ID__c=ex.ContextRecordId;
        excep.Exception_Details__c =  ex.ErrorMessage;
        ecelist.add(excep);
    }
    
    if(ecelist.size()>0){
        insert ecelist;
    }
}

Comments

  1. Great work @Meera. Thank you for sharing your learning!

    ReplyDelete
  2. Great Post Meera, just a quick question - do we need to consider and limitations with using Custom Platform Events? Is there a way to check how many platform events we can have in an org and where we can check how are we doing on these numbers (whether we are getting close to this limits)

    ReplyDelete
    Replies
    1. Below link has limits related details of platform event.
      https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_event_limits.htm

      From high level, when external systems are consuming platform events - REST API - MonthlyPlatformEventsUsage will give us limit related details.

      If we have Platform event add-on license, then Under setup->Company details->Usage-based Entitlements will show us PL limit related details.

      Using apex-> Limits class has below 2 methods available:
      getPublishImmediateDML() - Returns the number of EventBus.publish calls that have been made for platform events configured to publish immediately.

      getLimitPublishImmediateDML() - Returns the total number of EventBus.publish statements that can be called for platform events configured to publish immediately.

      Delete

Post a Comment

Popular posts from this blog

Subscribing to Salesforce Platform Events using External Java Client - CometD

Salesforce Security - Restriction Rules and Scoping Rules

How to develop reusable Invocable Apex methods for Flows