Can anyone tell me exactly when the calculation of formula field happens?
Thanks
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityIt's calculated on demand, and thus does not participate in the order of execution. In practice, that means it calculated each time it is queried from the database.
In the context of a DML operation, each formula is recalculated while loading Trigger.old and Trigger.new, before evaluating validation, assignment, escalation, and workflow rules, and while executing process builder, flow, and approval processes.
You can also request a reevaluation in Apex Code using recalculateFormulas
.
recalculateFormulas
- some gotchas - see salesforce.stackexchange.com/questions/100627/…
recalcualteFormulas
Hopefully this Knowledge article helps.
In what order are automation rules and Apex triggers processed? Knowledge Article Number 000005694
Description In what order are automation rules and Apex triggers processed?
Resolution The following is the order salesforce logic is applied to a record. Old record loaded from database (or initialized for new inserts) New record values overwrite old values System Validation Rules (If inserting Opportunity Products, Custom Validation Rules will also fire in addition to System Validation Rules) All Apex before triggers (EE / UE only) Custom Validation Rules Record saved to database (but not committed) Record reloaded from database All Apex after triggers (EE / UE only) Assignment rules Auto-response rules Workflow rules Processes Escalation rules Parent Rollup Summary Formula value updated (if present) Database commit Post-commit logic (sending email) Additional notes: There is no way to control the order of execution within each group above. Workflow field updates that run based on an approval process or time-dependent action do not trigger any rules. Formula fields do not execute in this way. They calculate and display their results real-time whenever the field is accessed in any way. So for example if a Workflow Rule uses a Formula Field in its criteria or formula, the formula field is evaluated when the Workflow Rule criteria is checked.
For additional details on the Order of Execution please review: https://help.salesforce.com/s/articleView?id=000327325&type=1