In Salesforce, there are two primary mechanisms to process a large chunk of records.
- Scheduled flows represent the declarative, no-code method released in Winter ’20, which utilises the popular Flow Builder user interface.
- Batch Apex, on the other hand, is the old-school programmatic approach which relies on Salesforce’s proprietary programming language, Apex.
I recently had to decide between choosing Batch Apex and Scheduled flows for a solution.
While my initial preference was to use Batch Apex, due to the nature of the requirement, I was working in an org where no-code solutions, such as Scheduled flows, had to be considered from all fronts prior to jumping to code-based solutions.
This scenario gave me a perfect opportunity to investigate the differences between the two.
My conclusion: Batch Apex beats Scheduled flows in 95% of all scenarios.
Here are the 4 reasons why:
- Complex Query Traversal
The main advantage of relational databases is the ability to leverage relationships between tables to gather specific data in one shot.
This is where Scheduled flows already start tripping.
Scheduled flows are limited to direct queries on the singular selected object at point of creation.
This means you cannot leverage relationship fields on the chosen object in the Flow to define cross-object, deep complex queries.
Example. I wanted to query OpportunityLineItem which also looked up data related to its Opportunity and Product associations. This is not possible in Scheduled flows without creating background cross-object formula fields.
Meanwhile, Batch Apex shines in its ability to handle complex queries.
The same query can be easily written in SOQL:
SELECT Id, Quantity, UnitPrice, Opportunity.StageName, Product2.Family
FROM OpportunityLineItem
WHERE Opportunity.StageName = 'Proposal/Price Quote'
AND Product2.Family = 'Software'
Furthermore, the choice of writing dynamic SOQL or static SOQL is also at your disposal when writing Apex.
All this is particularly useful when dealing with large datasets where nuanced data relationships play a crucial role in the data processing logic.
- Record Processing Scale
Each Scheduled flow interview can query up to 50,000 records per transaction.
Meanwhile, a single batch job in Batch Apex can process up to 50 million records.
You can control the size of each batch too in Apex (the number of records processed in each execution of the execute
method), with a maximum of 2,000 records per batch.
Thus, if you are working with large data volumes, there really is no comparison.
- Scheduling Options
Scheduled flows are limited to run only at predefined intervals.
This means that you have to choose to start the flow on a particular date and time, and specify how often it repeats with the option of only a once, daily, and weekly run available.
Meanwhile, you can get as dynamic with Batch Apex as you want.
- Batch Apex can be scheduled programmatically using Apex to execute at specific intervals e.g. multiple times of the day, or even in response to specific events – like the completion of another batch job.
- Batch Apex also uses cron expressions for scheduling, which provides extensive control over how and when the jobs can be run.
- Finally, Batch Apex jobs can be chained; meaning one job can trigger another upon completion, allowing for sequential data processing tasks. This is particularly useful for complex workflows.
- Overall Flexibility
Scheduled flows are incredibly fiddly to edit. I am saying this despite being a proficient Flow builder.
They often involve workarounds that lead to unnecessary complexity and increased technical debt.
Example. I wanted to use TODAY()
in my scheduled flow.
But guess what? I was unable to. This is because in scheduled flows, to use TODAY()
you first need to create a formula field that captures this. This field must then be referenced within the flow.
This approach meant not only adding extra steps but also cluttering the database schema with additional fields that serve a single calculation purpose, thereby increasing overall maintenance overhead too.
In contrast, in Batch Apex, to define this it was as simple as:
Date today = System.today();
This ease of incorporating dynamic data directly into the logic without any additional setup is a prime example of the overall flexibility that Batch Apex provides in multiple scenarios.
Now to conclude.
In my brief investigation between the two tools, Batch Apex proves superior to Scheduled flows in handling complex, large-scale data processing tasks within Salesforce.
If you are building a throwaway PoC, Scheduled flows can be of use.
But, if you are looking for robust capabilities which allow for intricate queries, scalable data volume management, and highly customisable scheduling options, without the need for cumbersome workarounds, Batch Apex must be your preferred choice.
Ultimately, Batch Apex not only meets but exceeds production needs, providing a reliable and dynamic solution where Scheduled flows fall incredibly short for now.