Custom Metadata Type Switches for Bypassing Record Trigger Flows

Custom Metadata Type Switches for Bypassing Record Trigger Flows

Often when I teach Apex and Flow triggers I get a common discussion in the classes. They are usually something like this: “My triggers work great until we have to load data.” Salesforce users often face challenges when importing large datasets into their Salesforce orgs. Apex and Flow triggers are fired during the data loading process Even though your triggers are properly bulkified you could start running into governor limit exceeded issues.

I’ve heard many stories on how admins and developers would try to solve this problem by inactivating the triggers. Unfortunately this is a very manual and error prone activity.

A simpler approach is to define a Custom Metadata Types as a trigger switch and build bypass logic into all of your triggers. In this article I will discuss bypass logic for record trigger flows. I will also show how. we can use an invocable apex call to simplify out bypass logic in a record flow trigger.

To give some historical perspective on this technique, when I worked in the financial industry many years ago working with batch Cobol, all of our code included bypass logic to use if there was a problem record as input.

Custom metadata types are a powerful feature that allows you to create customized setup objects where the records are treated as metadata instead of regular data. This is particularly useful when you need to migrate application configurations between different environments or package and install them. By leveraging custom metadata types, you can build declarative developer frameworks for internal teams, partners, and customers, enabling them to define and drive their own types of metadata-driven applications.

Metadata, in this context, refers to the information that describes the configuration of each customer's organization. Within a record trigger flow, you can query a custom metadata type using a Get Element operation. However, it's important to note that this currently counts against the limits of SOQL (Salesforce Object Query Language).

To address the SOQL Limit issue, the Salesforce Flow product team is actively working on a solution. In the meantime, an alternative approach is to utilize an Apex call to query a custom metadata type without impacting the SOQL limits. This allows you to perform the necessary operations while ensuring the efficient usage of resources.

The first demo will present the following:

  1. A custom metadata type to enable/disable a trigger
  2. A subflow that reads the custom metadata type record and returns enabled/disabled information
  3. A record trigger flow that calls the subflow for bypass information

The second demo will present the following:

  1. An invocable apex class that returns enabled/disabled information
  2. A record trigger flow that calls the invokable apex class for bypass information

The approach will be to create a custom metadata type called Trigger Configuration (Trigger_Configuration__mdt). This will have a field for the trigger name, bypass user, and a check box field to indicate if the trigger is enabled.?

No alt text provided for this image

Next I created a record of data to to allow bypass logic in a record trigger flow called: New_Account_Default_Opportunity. Bypass User contains the username of the running user that can potentially bypass the execution of the trigger.

No alt text provided for this image

Next I wrote a subflow called Bypass Switch Subflow that uses a Get Element to query Trigger_Configuration__mdt for the appropriate record based on a user name and the trigger name. The Get returns the Is_Trigger_Enabled__c value. the subflow returns a true/false based on the state of Is_Trigger_Enabled__c. Note that the subflow is an Autolaunched flow.

No alt text provided for this image

For the demonstration I wrote a simple record trigger flow that will create a default opportunity every time a new account is created. This is Create, Action and Related Records trigger flow. The other type of flow is a Fast Field update. (As a side note, we have been referring to Fast Field update (FF) as french fries and Action and Related Records (ARR) as pirates because those terms are simpler to say and they are fun.) WE can only call subflows and invocable apex from pirates, not french fries. If you need to bypass a french fry you must add the bypass logic directly into your flow.

No alt text provided for this image

Calling the subflow requires us to provide the name of the trigger as defined in the custom metadata record. I use the global variable $User to provide the current running user’s username. isEnabled is a boolean value to hold the true/false value returned from the subflow.

No alt text provided for this image

Adding the subflow element and the decision element to all of your pirate record trigger flows allows you to have the option of bypassing the triggers without out having to deactivate the triggers.

One of the caveats of using a Get Element to query Trigger_Configuration__mdt is that the call goes against your SOQL governor limits at this time. If this is a concern then we can create an invokable apex class with invokable variables. In this apex class we can we can use custom metadata type methods to read our record data without the call going against the SOQL governor limits. In the apex class we call the public CustomMetadataType__mdt getInstance(String developerName) to return a CustomMetadataType__mdt record.

Here is an example of the apex class:

No alt text provided for this image

The import items to note:

  • @InvocableMethod(label='Get Trigger Bypass Switch' description='Returns True/False bypass switch' category='Switch')
  • Allows the apex class to be accessable from flow
  • Trigger_Configuration__mdt varMySwitch = Trigger_Configuration__mdt.getInstance(input.triggerName);
  • The custom metadata type method call to get our record
  • global class FlowInput
  • contains the @InvocableVariables that are exposed in the flow

The change to our flow is very simple:

No alt text provided for this image

Calling the Apex Action is very similar to calling the subflow:

No alt text provided for this image

Whether you choose to use the Get Element operation in a subflow or call an invocable Apex class with custom metadata type methods, you have the power to overcome the challenges of bypassing a trigger during a data load. Salesforce administrators and developers can harness the flexibility and customization offered by Custom Metadata Types, subflows, and invocable Apex classes to streamline trigger execution and ensure a smooth data loading experience.

Antoine Robitaille

Certified Salesforce PDII Developer

4 个月

Pretty neat! I stumbled upon this while reading about bypassing flows using Custom Settings. What are the Pros and cons of both approaches? CMDT vs Custom Settings? What cam up with so far: CMDT Pros --> Deployable Cons --> More difficult to change on the fly. Can't be directly used in Triggering criterias Needs an apex invocable called IN the flow to avoid SOQL limit More Complex if you want to Have a variation of granularity , By users, By Profiles Hierarchical Custom Settings: Pros -->Can be accessed "at no cost" in any formula, validations rules, duplicate rules, flows entry criteria. Offers Granularity Org Wide, By Profiles or Users, Cons--> Can't be deployed; would need to be populated using apex post deployment OR post Seeding. In all cases, we then need to refactor the hundreds of flows and triggers,. BUt this could be donne gradually, when the need to by pass is there. Am I missing a point? cheers

  • 该图片无替代文字
回复
Mahesh Somineni

Salesforce Certified System and Application Architect at Cognizant, aspiring to become a CTA

1 年

I think that the Hierarchical Custom Setting is a better fit compared to Custom Metadata. It provides greater capabilities than custom metadata for these specific requirements. It can be managed across the entire organization, as well as at the profile and individual user levels. Any specific reason you have chosen custom metadata than Hierarchical Custom Setting?

回复
Rajat Koradiya

Helping Enterprises Transform Their Revenue Lifecycle | Conga & Salesforce Revenue Cloud Architect

1 年

We have done that in our organization using hierarchy?custom setting. There we can assign org wide default as well as set the profile level access along with user access. that will be useful if you want to disable it for entire profile for some time that we can do it.

Julian Marino ?

Technical Architect at Salesforce | Systems Engineer | EU Citizen/Ciudadano UE

1 年

Very useful, but in these cases I think custom permissions is a better solution, you can control who avoid triggers even at user level. With this approach you can avoid problems with the integrity of the data.

回复

This is a great idea thank you for sharing! JoAnn Culbertson - check out this post!

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了