Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 36 ยท Objects, Fields and Data Model

Formula Fields and Roll-Up Summary Fields

Write Salesforce formula fields with IF, CASE, text, date and cross-object functions, avoid blank-value errors, and summarize child records with roll-up summary fields.

Formula fields and roll-up summary fields calculate values for you. A formula field computes from fields on the same record (or a parent record) every time it is displayed. A roll-up summary calculates from child records. Both are declarative: no code, no manual updates, and the value is always current.

Formula fields

Create one under Object Manager > object > Fields & Relationships > New > Formula. You choose the return type (Text, Number, Currency, Percent, Date, Date/Time, Checkbox) and then write the formula in the editor. Use Check Syntax before saving. A formula field is read-only for users and is recalculated on the fly, so it does not use storage the way a normal field does and never goes stale.

Limits: a formula can have about 3,900 characters, and the compiled size must stay under about 5,000 characters. It can also reference a limited number of other formulas. Complex logic belongs in a flow.

Operators and basic functions

  • Math: + - * / ^, ROUND(x, 2), ABS(), MAX(), MIN(), MOD().
  • Text: & joins text, LEFT(), RIGHT(), LEN(), TEXT(), UPPER(), CONTAINS(), SUBSTITUTE(), TRIM().
  • Logic: IF(test, then, else), AND(), OR(), NOT(), CASE(expression, v1, r1, v2, r2, else), BLANKVALUE(), NULLVALUE().
  • Date: TODAY(), NOW(), DATE(y, m, d), YEAR(), MONTH(), ADDMONTHS(), date1 - date2 (gives days).
  • Picklist: ISPICKVAL(Field, "Value") for tests, TEXT(Field) to use the value as text.
  • Blank checks: ISBLANK(field).

Examples:

Text
Amount * 0.1

returns a 10 percent commission (Currency).

Text
IF(Amount >= 100000, "Large", IF(Amount >= 10000, "Medium", "Small"))

returns a size band (Text).

Text
CloseDate - TODAY()

returns days until close (Number, negative when late).

Text
CASE(StageName, "Closed Won", 1, "Closed Lost", 0, 0.5)

maps a picklist to a number. CASE is cleaner than nested IF when you test one field against many exact values.

Text
IF(ISBLANK(Phone), "No phone", Phone)

shows a fallback for a blank field.

Blank values and the "treat blank as zero" option

When you save a Number, Currency or Percent formula, the wizard asks how to treat blank fields: Treat blank fields as zeroes or Treat blank fields as blanks. With "blanks", the whole formula returns blank if any input is blank. With "zeroes", blank counts as 0. This changes results. Percentage formulas dividing by a blank denominator can also give an error, so guard with IF(Total = 0, 0, Part / Total).

Python Stand-in: evaluate a formula with a safe divide

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Cross-object formulas

A formula can read fields from a parent through the relationship: Account.Industry, Account.Owner.Manager.Name, or Project__r.Budget__c. You can span up to 10 relationships, but each span adds cost and may hit limits. Only the "up" direction is possible: a child can read its parent, but a parent formula cannot read its children (that is the job of roll-ups).

Text
Account.AnnualRevenue * 0.02

on an Opportunity estimates a budget from the account's revenue.

Common formula mistakes

  • Comparing a picklist with =: use ISPICKVAL(Status, "New").
  • Mixing types: join text and numbers with & and TEXT(number).
  • Forgetting that a Date minus a Date gives days (a Number), but a Date/Time minus another gives days with a fraction.
  • Time zone confusion: TODAY() uses the running user's time zone, NOW() is a point in time.
  • Using formula fields for reports on huge data volumes: formulas are calculated at query time and are not indexed by default.

Roll-up summary fields

A roll-up summary field lives on a master record and summarizes its detail records (master-detail only). Four kinds exist:

  • COUNT the detail records
  • SUM a Number, Currency or Percent field
  • MIN or MAX of a number, currency, date or date/time field

You can add a filter to include only some details, such as only opportunities with Stage "Closed Won". Create one under the master object: Fields & Relationships > New > Roll-Up Summary, select the detail object, the type, the field and the criteria.

Examples: a Project object (master) can roll up Total Hours as the SUM of Hours on its Time Entry details, and Open Tasks as the COUNT of Task Item details where Status is not Done. (Standard Account-to-Opportunity is a lookup, so it has no roll-up summary field; use a flow or a report there.)

Limits: up to 25 roll-up summary fields per object (edition dependent). Roll-ups update automatically when details change; changing a field a roll-up uses can lock in recalculation, and Setup has a Recalculate action if a roll-up looks stale.

Python Stand-in: roll-up summary over detail records

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When a lookup needs roll-ups

If your relationship is a lookup, you cannot create a roll-up summary field directly. Options: convert to master-detail (if your data allows it), use a Record-Triggered Flow to calculate and store totals, use a report, or install a declarative lookup roll-up tool. A flow with a Get Records and an aggregate calculation works for modest volumes.

Formula or roll-up or flow?

Need Tool
Calculate from this record or its parents Formula field
Summarize child records (master-detail) Roll-up summary
Summarize children on a lookup, or store a value Flow
Complex multi-step logic Flow or code

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function should you use to test the value of a picklist field in a formula?

    Picklist fields cannot be compared with = directly; use ISPICKVAL(Field, "Value") or TEXT(Field).
  2. 2. On which object do you create a roll-up summary field?

    Roll-ups summarize child records and live on the master.
  3. 3. A formula divides Part by Total and errors when Total is zero. What is a safe pattern?

    Test the denominator first so the formula never divides by zero.
  4. 4. Which aggregate types do roll-up summary fields support?

    Roll-up summary fields offer COUNT, SUM, MIN and MAX. Averages need a formula built from SUM and COUNT.

Summary

  • Formula fields calculate from the record and its parents; they are read-only and always current.
  • Handle blanks and zero denominators explicitly, and use ISPICKVAL for picklists.
  • Roll-up summaries (COUNT, SUM, MIN, MAX) work on the master of a master-detail relationship; use flows for lookups.
  • Keep formulas readable, short and documented.

Next: Validation Rules