Sub topic 10
A practical framework for choosing
This chapter assumes you know the Salesforce data model, can find your way around Setup, have built a simple flow, and understand variables, loops, and collections. Earlier chapters covered what Apex and Flow are, where each fits, how governor limits and bulk-safe design apply to both, and how testing and maintenance differ. Here you combine those ideas into a repeatable way to decide.
Start with Flow, then look for reasons to leave
Flow is the right first choice for most record-triggered, screen, and scheduled automation. It is faster to build, easier for admins to read, and cheaper to maintain. Treat Apex as the answer when a specific requirement rules Flow out, not as the default.
The framework is a short series of questions. Ask them in order and stop at the first one that gives a clear answer.
- Can a flow do this with standard elements (Get Records, Update Records, Decision, Loop, Assignment, Action)?
- Does the requirement need something Flow cannot express cleanly?
- Will the volume or complexity put the transaction near governor limits?
- Who will maintain it, and how will it be tested and deployed?
- Is a hybrid the cleanest result?
Signals that point to Apex
Choose Apex when one or more of these is true.
- The logic needs complex data structures, such as nested maps of lists, or recursion.
- You need fine control over transactions, such as savepoints and rollbacks, or partial-success DML with
Database.update(records, false). - You must process very large volumes with Batch Apex or Queueable Apex, or need precise control over asynchronous behavior.
- You need a custom REST or SOAP endpoint, or a callout with custom headers, signing, or response parsing.
- The same logic must be reused from triggers, Lightning components, and APIs.
- The flow would need dozens of elements and nested loops that a developer can express in a few readable lines.
- You need automated unit tests with assertions that run on every deployment.
Signals that point to Flow
Stay with Flow when these hold.
- The work is field updates, record creation, notifications, approvals, or guided screens.
- An admin team owns the process and changes it often.
- The logic is a handful of branches that a business analyst can read on a canvas.
- You can use a before-save record-triggered flow for same-record field updates, which is very fast.
Reading a requirement
The table applies the questions to typical requirements.
| Requirement | Choice | Reason |
|---|---|---|
| Set Priority to High when Amount exceeds 100,000 | Flow (before-save) | Same-record update, simple condition |
| Send a Chatter post when a case escalates | Flow | Standard action, admin-owned |
| Roll up the contact count to the account, with custom grouping | Apex trigger | Aggregate query, reusable, unit-testable |
| Call a signed external API and parse nested JSON | Apex | Custom callout and parsing |
| Collect inputs on a screen, then apply a discount using shared logic | Flow plus invocable Apex | Flow owns the screens, Apex owns the calculation |
Applying it to a real case
Suppose sales operations wants to keep Contact_Count__c on Account current. The logic is an aggregate over related records. A flow can do it, but it must loop and query carefully to stay bulk-safe. A short Apex trigger handles it with one aggregate query and one update, and a test inserting 200 contacts proves it. If the team has developers and the field drives reports, Apex is a reasonable choice. If no developer is available and volumes are small, a flow is acceptable, as long as you avoid queries and DML inside loops.
Common mistakes
- Choosing Apex because it feels more powerful, then leaving admins unable to adjust a simple rule.
- Choosing Flow for a requirement that needs many nested loops, then hitting a limit in production.
- Deciding on build speed alone and ignoring test and deployment cost. Apex requires tests and at least 75% overall coverage for production, while flows are not tested the same way.
- Skipping the ownership question. The best tool is one the owning team can support.
Exercise
A requirement says: "When an opportunity is closed won, copy the Amount to a custom field on the same record and notify the owner." Which tool do you choose, and which signal decides it?
Show answer
Flow. The work is a same-record field update plus a notification, both standard flow capabilities. There is no need for complex structures, custom callouts, or heavy volume, so no signal points to Apex. A record-triggered flow, with the field copy in a before-save flow where possible, is the simplest and most maintainable choice.
Exercise
A team needs a screen flow where users enter a discount percent, and the discount rules are shared with a nightly batch job. What is the best design?
Show answer
Use both. Build the screen in a flow, and put the discount calculation in an Apex invocable method that accepts a list of requests. The flow calls the method as an action, and the batch job calls the same Apex logic, so the rules exist in one place.