Sub topic 8
Formula size and relationship limits
This chapter covers the limits that cause formula errors in practice: how large a formula can be, how far it can reach across related records, and how to fix a formula that exceeds either limit. It assumes you can write formulas with IF, CASE, and BLANKVALUE. It also assumes you know how to use relationship names such as Account.Owner.Name to read fields on related records.
Two different size limits
Salesforce measures a formula in two ways. A formula must pass both checks.
| Limit | Value | What it measures |
|---|---|---|
| Formula text | 3,900 characters | The expression you type, including spaces and line breaks |
| Compiled size | 5,000 characters | The expression after Salesforce expands it internally |
| Text result | 1,300 characters | The longest string a Text formula can return |
The compiled size is the one that surprises people. Salesforce replaces every field reference with its internal form, and a reference to another formula field is expanded into that formula's full logic. A short expression can therefore compile to something large.
The editor reports a failure when you save. A typical message looks like this:
Error: Compiled formula is too big to execute (6214 characters). Maximum size is 5000 characters.The number in the message is the compiled size. Use it to judge how far over the limit you are.
What makes a formula grow
These patterns inflate compiled size the most:
- Deeply nested IF statements. Each level repeats the field references and comparison logic.
- Repeated field references. Writing
BLANKVALUE(Amount, 0)in every branch multiplies its cost. - Cross-object references.
Account.Owner.Manager.Namecompiles to several lookups, not one. - Chains of formula fields. If formula C uses formula B, and B uses formula A, C's compiled size includes both A and B.
- Long text literals. Strings count toward both limits.
Rewriting a formula to fit
Replace nested IF statements with CASE when you compare one field against several fixed values. Here is the nested version of a stage score:
IF(ISPICKVAL(StageName, "Prospecting"), 10,
IF(ISPICKVAL(StageName, "Qualification"), 25,
IF(ISPICKVAL(StageName, "Proposal"), 50,
IF(ISPICKVAL(StageName, "Negotiation"), 75,
IF(ISPICKVAL(StageName, "Closed Won"), 100, 0)))))The CASE version returns the same result with one field reference and no nesting:
CASE(StageName,
"Prospecting", 10,
"Qualification", 25,
"Proposal", 50,
"Negotiation", 75,
"Closed Won", 100,
0)CASE works on picklist fields directly, so you don't need ISPICKVAL. It only tests equality. For ranges such as "over 100,000", you still need IF.
For range tests, check the most selective condition first and avoid repeating expensive expressions. If a long expression such as a cross-object reference appears in several branches, ask whether a separate formula field would shorten the main formula. This helps only when the helper's logic is small. Because of expansion, a chain of formulas rarely reduces the compiled size of the final one.
Relationship limits
A cross-object formula reads fields from related records by following relationship names. Three kinds of limits apply.
Distance. A formula can follow a chain of lookup or master-detail relationships only a limited number of levels, currently up to 10 relationships from the starting object. Any single path that long is almost always a design problem, and it also uses up compiled size.
Number of unique relationships. Each object has a cap on how many distinct relationships its formula fields can reference in total. Two formulas that both use Account.Owner share one relationship. A formula that uses Account.Owner and another that uses Account.Parent use different ones. Salesforce raises an error when a new formula would exceed the object's cap. Check the current value in the Salesforce documentation, because it changes between releases.
Direction. A formula can follow a relationship from child to parent, such as Opportunity to Account. It cannot read from parent to child. To total or count child records, use a roll-up summary field on a master-detail relationship, or use code, as covered in later chapters.
Other restrictions that look like limits
Some errors have nothing to do with size:
- A formula cannot reference a long text area field.
- Polymorphic relationship fields, such as
WhoandWhaton Task, support only a few standard fields likeNameandOwner. UseTYPEOFin SOQL or code when you need more. - Formula fields cannot be edited in DML. Writing to one in Apex fails at compile time.
- A formula can expose data the viewing user cannot otherwise see. Relationship traversal ignores the user's access to the related record, so review what you display.
- Deleting or changing a field that a formula uses is blocked until you remove the dependency.
Diagnosing a failed save
When the formula cannot be reduced, the standard fix is to calculate the value elsewhere and store it in a regular field. A record-triggered Flow or an Apex trigger can write the value once when the data changes. You lose the automatic recalculation of a formula, but you remove the size limit. Chapter 17 compares these options in detail.
Checking compiled size while you work
You can't see the compiled size until a save fails, so build large formulas in steps:
- Write and save the smallest working version.
- Add one branch or one cross-object reference at a time.
- Save after each addition so that a failure points to the last change.
- Note the reported size when it fails, then simplify before adding more.
Test with the Check Syntax button before saving. It catches syntax mistakes, though in some cases the size error appears only on save.
Exercise
A Text formula field on Contact uses Account.Owner.Manager.Name and a long chain of nested IF statements. Saving fails with "Compiled formula is too big to execute (5400 characters)". The formula text is only 2,800 characters. Why does it fail, and what two changes would you try first?
Show answer
The 3,900-character text limit is satisfied, but the compiled size limit of 5,000 is not. Compilation expands each field reference, including the cross-object path, and each nested IF repeats that logic. Try replacing the nested IF statements with CASE where the tests are equalities, and remove repeated references so each expression appears once. If it still exceeds 5,000, store the calculated value in a regular field with a record-triggered Flow.
Exercise
Why is splitting a large formula into several smaller formula fields an unreliable way to reduce compiled size?
Show answer
When one formula field references another, Salesforce expands the referenced formula inside the referencing one, so the compiled size of the final formula still includes all of the logic. Splitting helps readability, but it does not reliably get you under the 5,000-character limit.