Skip to main content

SF-0261 · Scenario · Easy

When should we use apex?

✓ Verified by Vikas Singhal · Last reviewed 5/17/2026 · Updated for Spring '26

The official Salesforce guidance is unambiguous: use declarative tools first, Apex only when configuration cannot solve the problem. Flow, validation rules, formula fields, and approval processes should be your first stop. Apex is the escape hatch when the platform’s no-code surface can’t express the rule cleanly, safely, or performantly.

When Apex is the right tool

Reach for Apex in these scenarios:

  • Complex business logic that Flow would model with deeply nested decisions, fault paths, and assignments — at some point Flow becomes harder to maintain than a well-written handler class.
  • Callouts to external systems where you need fine-grained control over the HTTP request, retries, response parsing, or named credentials — HttpCallout and HttpRequest give you this. (Flow’s HTTP Callout action is fine for simple GETs; it isn’t for OAuth flows or complex payloads.)
  • Bulk operations beyond Flow’s limits — Flow has its own governor limits, and processing 200 records with many SOQL queries inside a Flow loop will fail. Apex with proper bulkification handles 10,000+ rows in batch jobs.
  • Asynchronous work@future, Queueable, Batch Apex, and Schedulable are the only ways to defer expensive work off the synchronous transaction.
  • Custom REST or SOAP endpoints — exposing your org as a service via @RestResource or @WebService requires Apex.
  • Lightning Web Component server methods@AuraEnabled controllers run Apex on the server for LWCs that need data the wire service can’t fetch.
  • Trigger logic that runs before/after DML events on objects.

When NOT to use Apex

Apex carries real costs:

  • Test coverage — you cannot deploy Apex to production without 75% coverage. Flows have no such requirement.
  • Maintenance debt — admins can edit Flows; only developers can change Apex.
  • Governor limit exposure — bad Apex blows limits and breaks transactions; bad Flow usually fails more gracefully.

If a validation rule, formula field, or simple Flow can do the job, do not write Apex. The Salesforce architect community calls this the clicks-not-code principle, and it holds up across release after release.

A decision shortcut

RequirementUse
Field validationValidation rule
Derived value from same recordFormula field
Roll-up on a master-detailRoll-up summary
Record updates on eventsFlow (Record-Triggered)
Multi-step business process with branchingFlow
Cross-system callouts beyond simple HTTPApex
Bulk data processing (> 2,000 records)Batch Apex
Scheduled jobsSchedulable Apex
Custom REST/SOAP APIApex @RestResource
LWC server-side data@AuraEnabled Apex
Trigger logic beyond Flow’s record-triggered scopeApex Trigger

What interviewers are really looking for

The signal here is judgement. A developer who reaches for Apex first looks expensive — they’ll write code where a formula would do. A developer who knows when to step up to Apex and why is the one teams want. Mention test coverage, maintenance handoff to admins, and Flow’s bulkification limits, and you’ve answered the real question.

Verified against: Apex Developer Guide — Introducing Apex, Trailhead module Apex Basics for Admins. Last reviewed 2026-05-17 for Spring ‘26 release.