Flow best practices: bulk, limits, reuse, testing and deployment
Avoiding queries and DML in loops, collection processing, subflows and invocable Apex, naming and documentation, debug and Flow tests, versioning and deployment.
Simple explanation
Performance and limits (flows share the transaction's governor limits)
- No Get Records, Create, Update or Delete inside loops. Build collections in the loop, then do one DML after it.
- Use Transform, Filter and Sort collection elements instead of hand-written loops where possible.
- Get only the fields and records you need; set limits on Get Records.
Reuse and maintainability
- Subflows for shared logic; invocable Apex for complex logic, callouts or performance-critical work.
- Consistent naming (
Object β Purpose), descriptions on every element, and custom metadata or custom labels for thresholds and text. - Avoid hardcoded IDs; look up record types, queues and users by developer name.
Quality
- Debug with realistic records, including bulk updates (data loader in a sandbox).
- Flow tests for record-triggered flows (with assertions), plus Apex tests that exercise flows through DML.
- Fault paths and error logging.
Delivery
- Flows are metadata: source control, deploy through the pipeline, and activate the intended version.
- Keep the number of active versions and inactive clutter low; document changes.
Real-world analogy
A flow with Get Records inside a loop is a shopper who drives to the supermarket separately for each item on the list. Collections are the shopping trolley: gather everything, then check out once.
Worked example
Updating all open cases for accounts that became inactive:
- β Loop over accounts β Get Records (cases for this account) β loop β Update Records each case.
- β Get Records: all open cases where
AccountIdis in the inactive accounts collection (one query) β Loop to setStatus = Closed β Account Inactiveand add to a collection β one Update Records on the collection after the loop.
Diagram
Key points
- No queries or DML inside loops; collect, then act once.
- Use collection elements (Transform, Filter, Sort).
- Subflows and invocable Apex for reuse and heavy logic.
- Naming, descriptions, no hardcoded IDs, configuration in metadata.
- Flow tests, bulk debugging, fault paths.
- Source control and pipeline deployment for flows.
Limits and gotchas
- Flows consume the same SOQL, DML and CPU limits as Apex in the transaction.
- Element execution limits per interview exist; extremely long loops fail β verify current limit.
- Screen flows that call Apex run as the user unless configured otherwise; check security.
- Too many active flows on one object make debugging and performance hard; consolidate.
π€ How to say this in an interview
Show you treat Flow as code: bulk-safe design, reuse, testing, error handling and CI/CD. Interviewers like the "Get Records in a loop" anti-pattern and its fix.
Common follow-ups
When does a flow need invocable Apex?
For complex algorithms, callouts with robust error handling, heavy data processing, or logic that must be unit-tested with mocks. Keep the invocable bulk-safe (lists in, lists out).
How do you test flows?
Flow tests for record-triggered flows with assertions, debug runs with realistic data, Apex tests that perform DML to exercise flows in bulk, and UAT for screen flows.