Developer Dilemma... Flow vs Apex : Set collection
The ongoing struggle between Flow and Apex for automating tasks in Salesforce is a constant challenge for developers and architects, with no clear-cut answer. It all depends on various factors, and the decision we make today may not be relevant or correct in the future as clients extend their solutions to new business units or geographical regions, so its important to look at future scalability and design accordingly.
As developers, we typically lean towards configuration-based solutions for simpler tasks, following Salesforce best practices. But every now and then, we stumble upon a requirement that seems straightforward. That's what happened to me recently when I encountered a task that seemed perfect for record-triggered automation with flows.
This design is essentially split into two execution contexts. The first part is straightforward: order line updates need to trigger rollup calculations at the order level. To achieve this, we can use a simple record-triggered flow to collect unique order Ids and fire a platform event at the end. But to my surprise, Lightning flows currently lack native support for Set<Id>.
Set<Id> plays a significant role in optimizing Salesforce processing, especially in scenarios involving hundreds or thousands of order line items. When order lines are updated in bulk, it makes sense to perform calculations at the order level only once, rather than for every instance of an order line update. This is easily accomplished with Apex triggers, as we can collect Order Ids in a Set and internally handle calculations on unique sets of Ids.
Now, let's dive into the debate of Flow vs Apex Code. Currently, Flow lacks native support for Set<Id>. Let's explore how poorly Flow performs when we have to implement a workaround to ensure record id uniqueness.
Flow - workaround for Set:
You can ensure the uniqueness of Ids in a collection variable within flows by utilizing the "Remove All" and "Add" operators in Flow Assignment. However, it's important to note that this approach introduces additional performance overhead as a tradeoff.
Time to test the performance:
Comments
Post a Comment