Salesforce Validation Rule vs Flow vs Apex Trigger Required Field Performance Benchmark : Luke Freeland

Salesforce Validation Rule vs Flow vs Apex Trigger Required Field Performance Benchmark
by: Luke Freeland
blow post content copied from  Blog
click here to view original post


In the “Before Times”, the only Salesforce Object validation options were Validation Rules and Apex Triggers. In a recent release, Salesforce enhanced record-triggered Flows so they also allow custom validation, which is a great addition! This had me wondering… What’s the performance like between them so I benchmarked it…

Methodology

Three custom objects were created. One for Validation Rules, the second for Flows, and the third for an Apex Trigger. Each object has a Text(255) field on it named Test that is optional. Each validation option was then used to make that Test field required. Apex code was then executed via Execute Anonymous in my personal Dev Edition org to insert 200 records into each object and record the time taken to do so in milliseconds. Each Validation option had 10 runs.

Validation Rule Methodology

A validation rule was created on the “Validation” custom object with the following definition:

Flow Methodology

A before-save record-triggered flow was created on the “Flow” custom object with the following definition:

Custom Error Definition:

Apex Trigger Methodology

An Apex Trigger was created to run on before insert on the “Trigger” custom object. Its logic checks to see if the Test field is blank. If so, then the Test field’s addError function is invoked requiring the field to be populated. Here’s the trigger:

trigger TriggerRequiredValidationTrigger on Trigger_Required_Validation__c (before insert) {
    for (Trigger_Required_Validation__c newRecord : Trigger.new) {
        if (String.isBlank(newRecord.Test__c)) {
            newRecord.Test__c.addError('Test is required.');
        }
    }
}

Benchmark Results

Validation Rule Flow Trigger
53.4 milliseconds 78.6 milliseconds 53 milliseconds

Disclaimer: These results are from a personal developer edition org running on the Winter 25 release. These timings will vary according to your edition, system load at the time, and other factors. Treat these as relative performance timings and not absolute timings.

Validation Rule Results

Run # Time in Milliseconds
1 54
2 61
3 43
4 54
5 58
6 69
7 54
8 51
9 46
10 44

Average Time: 53.4 milliseconds

Flow Results

Run # Time in Milliseconds
1 75
2 83
3 87
4 82
5 79
6 82
7 74
8 77
9 72
10 75

Average Time: 78.6 milliseconds

Apex Trigger Results

Run # Time in Milliseconds
1 66
2 43
3 60
4 53
5 55
6 53
7 49
8 47
9 48
10 56

Average Time: 53 milliseconds

My Interpretation

From a performance perspective, validation rules and an Apex trigger take approximately the same amount of time for a single field required validation. The Flow validation took the longest of the 3 options, which I was expecting but wasn’t sure. I was expecting that based on some of my previous benchmarks. My speculation is that the Flow engine has a higher overhead compared validation rules and Apex Triggers.

Guidance

While performance is one consideration when determining which validation option to use, it shouldn’t be the primary one. Easy administration is often the starting factor for me so I tend toward this pecking order:

  1. Validation Rule – Whenever possible, use a validation rule first. It’s great for same record validation or having to reference parent records. However, it can’t reference child records. They’re also easy to turn off and admins and developers alike can manage them.
  2. Flow – For more complex logic, use a flow. This can be defined on a before-save OR after-save record-triggered flow. My preference is to have 1 validation flow per validation so it’s easier to deactivate each one individually if needed AND it’s easier to understand too. This does require a slightly higher skill set but many admins and developers can manage them.
  3. Apex Trigger – For the most complex validation, use an Apex Trigger. It has excellent performance and can do any validation necessary. Coupled with a good Trigger Framework, this makes it easy to manage and maintain. However, this requires a developer.

What do you think?


December 05, 2024 at 02:38AM
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