Developer Fundamentals · Topic 2 of 4
Declarative versus Programmatic Customization
When to use clicks or code on Salesforce: formula fields, roll-up summaries, Flow versus Apex, and how governor limits shape a developer's choice.
- 5 min
- Prefer declarative tools when they meet the requirement.
- Formula fields are calculated when read and can reach parent fields, not children.
- Roll-up summaries work only on master-detail, with COUNT, SUM, MIN and MAX.
What it is
Salesforce can be customized with declarative tools (clicks: fields, formulas, validation rules, Flow, page layouts) or programmatic tools (code: Apex, SOQL, Lightning Web Components, Visualforce).
The platform's guidance is clear: use declarative tools when they meet the requirement, because they are faster to build, easier to maintain and upgrade safely. Use code when the requirement is beyond what clicks can do.
How it works
Choosing the tool
| Requirement | Best tool |
|---|---|
| Show a value calculated from fields on the same record or its parents | Formula field |
| Count, sum, min or max of child records in a master-detail relationship | Roll-up summary field |
| Block a save when data is invalid | Validation rule |
| Update fields on the same record when it is saved | Before-save record-triggered flow |
| Create or update related records, send email, call actions | After-save record-triggered flow |
| Roll up child records over a lookup relationship, or complex aggregates | Apex trigger (or Flow) |
| Complex logic, large data volumes, callouts with custom error handling | Apex |
| A custom, interactive user interface | Lightning Web Component |
Formula fields
- Read-only fields calculated each time the record is read; nothing is stored.
- Can reference fields on parent records through relationships (cross-object formulas), such as
Account.Industryon Contact. - Cannot reference child records; use a roll-up summary or code for that.
- Limits apply: a formula can hold up to 3,900 characters, and its compiled size is limited to 5,000 bytes.
A developer trap: after insert in Apex, a formula field on the in-memory record is not recalculated. Query the record again, or call Formula.recalculateFormulas(), to get the value.
Opportunity o = new Opportunity(Name = 'Q4', StageName = 'Prospecting',
CloseDate = Date.today(), Amount = 100);
insert o;
System.debug(o.Margin__c); // null: formula not recalculated in memory
o = [SELECT Margin__c FROM Opportunity WHERE Id = :o.Id];
Roll-up summary fields
- Defined on the master of a master-detail relationship.
- Functions: COUNT, SUM, MIN and MAX, with optional filter criteria on the child records.
- Recalculated automatically when detail records change, as part of the save order of execution.
- Not available on lookup relationships. For those, use a record-triggered flow or an Apex trigger that aggregates the children.
How governor limits shape the decision
Declarative automation is not free: record-triggered flows and Apex triggers run in the same transaction and share the same governor limits. A flow's Get Records element counts toward the 100 SOQL query limit, just like a query in a trigger.
| Concern | Declarative | Programmatic |
|---|---|---|
| Build and maintain | Faster, visible to admins | Needs a developer and tests |
| Deployment | No Apex coverage needed; flow test coverage applies only if the org deploys flows as active | Apex needs 75% coverage to deploy to production |
| Complex logic and large volumes | Can become hard to read | Full control, bulk patterns, async processing |
| Limits | Shared with Apex in the transaction | Shared with flows in the transaction |
A worked example
A company wants each Account to show the number of open Cases, and Case has a lookup to Account. A roll-up summary field is not possible, because roll-ups need master-detail and Case cannot be the detail of Account.
The options are a record-triggered flow on Case that recounts open cases for the parent, or an Apex trigger on Case that collects Account Ids in a Set<Id>, runs one aggregate query and updates the accounts in one DML statement. If the rest of the logic is simple, the flow is the declarative first choice; if the org already has a Case trigger framework or the volume is large, the trigger fits.
When two answers both work, the exam usually prefers the declarative one, unless the question names a reason for code, such as a lookup roll-up, a callout or complex logic.
How the exam asks about this
- "Show the parent's field on the child record." A cross-object formula field.
- "Sum the amounts of detail records on the master." A roll-up summary field.
- "Count child records related by a lookup." Flow or an Apex trigger; roll-up summaries need master-detail.
What to remember for the exam
- Prefer declarative tools when they meet the requirement.
- Formula fields are calculated when read and can reach parent fields, not children.
- Roll-up summaries work only on master-detail, with COUNT, SUM, MIN and MAX.
- Flows and Apex share one set of governor limits per transaction.
- Formula values are not refreshed in memory after DML; requery or recalculate.
Building Apex for something a formula or validation rule can do adds code that needs tests, maintenance and deployment. The exam marks that as the wrong choice.
Common mistakes
Choosing a roll-up summary on a lookup relationship. It is only available on master-detail.
Assuming flows have their own limits. They share the transaction's limits with Apex.
Reading a formula field right after insert. The in-memory value is null or stale until you requery.
Check yourself
Which declarative feature totals detail records on their master?
A roll-up summary field, with COUNT, SUM, MIN or MAX.
Does a Get Records element in a record-triggered flow count toward the SOQL query limit?
Yes. Flows and Apex in the same transaction share the governor limits.
Why can't an Apex variable show a formula field's new value right after insert?
Formulas are calculated when the record is read, so the in-memory record must be queried again or recalculated.
Topic 2 of 4 in Developer Fundamentals
Test yourself on Developer Fundamentals
The full Salesforce Platform Developer question bank has 645 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Salesforce. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.