Formula Fields in Salesforce are custom fields that automatically calculate values based on other fields or data. They are powerful tools that help you customize your data, enforce business logic, and display information in a way that suits your organization’s needs.

In this tutorial, we’ll walk through the steps to create a formula field in Salesforce and provide examples to illustrate different use cases.

Understanding Formula Fields

Formula fields are read-only fields that derive their value from an expression you define. The formula can reference:

  • Other fields on the same object
  • Fields from parent objects (up to 10 levels)
  • System variables (e.g., TODAY, NOW)
  • Mathematical operations and functions

Benefits of Formula Fields:

  • Consistency: Ensures data is consistent and accurate across records.
  • Real-time Calculations: Values are updated automatically when referenced fields change.

 Creating Formula field in Salesforce ?

Go to Setup => Build => Create => Object => Select object => Custom Fields & Relationships => Click new => Formula.

Now we are creating Formula field for student object to calculate average of three subjects F = ( S1+ S2 + S3) /3.

Go to detail view of the object.

Go to Custom fields and relationships and create new.

Now select formula which is of type number and Click next.Formulae fields in Salesforce

  • Now provide all the details like field label and field name . For formula return type select with decimal places zero.
  • A formula editor is shown with two tabs. 1.Simple formula and Advanced Formula.

Select the tab Advanced Formula you can observe function with right extreme which can be used. While writing this formula in the editor we can see two field in inside fields and outside fields to refer to the fields in the object.

In the formula editor provide the Formula given.

Now check the visibility at the top to give accessibility of this field to every profile.

Finally Save it.

Now go to your Student object create record and fill S1, S2, S3 subject marks and save it. In editable view formula field is not available . In Detailed view of the record you can view Formula.

Example 1: Calculating Age from Birthdate

Suppose you want to calculate a contact’s age based on their birthdate.

Steps:

  1. Field Details
    • Field LabelAge
    • Field NameAge
    • Formula Return TypeNumber
      • Decimal Places0
  2. Formula DefinitionplaintextCopy codeFLOOR( ( TODAY() - Birthdate ) / 365.25 ) Explanation:
    • TODAY() returns the current date.
    • Birthdate is the field containing the contact’s birthdate.
    • Subtracting Birthdate from TODAY() gives the number of days.
    • Dividing by 365.25 converts days to years (accounting for leap years).
    • FLOOR() rounds down to the nearest whole number.
  3. Check Syntax
    • Ensure there are no errors.
  4. Save the Formula Field
    • Complete the remaining steps as outlined above.

Result:

  • The Age field now displays the contact’s age in years.

Example 2: Concatenating Text Fields

Create a formula field that combines the contact’s first and last name.

Steps:

  1. Field Details
    • Field LabelFull Name
    • Field NameFull_Name
    • Formula Return TypeText
  2. Formula DefinitionplaintextCopy codeFirstName & " " & LastName Explanation:
    • FirstName and LastName are standard fields.
    • The & operator concatenates strings.
    • " " adds a space between the first and last names.
  3. Check Syntax
    • Ensure there are no errors.
  4. Save the Formula Field
    • Complete the remaining steps.

Result:

  • The Full Name field displays the contact’s full name (e.g., John Smith).

Example 3: Conditional Logic with IF Statements

Display a custom message based on the account’s annual revenue.

Steps:

  1. Field Details
    • ObjectAccount
    • Field LabelRevenue Category
    • Field NameRevenue_Category
    • Formula Return TypeText
  2. Formula DefinitionplaintextCopy codeIF(AnnualRevenue > 1000000, "High Revenue", "Standard Revenue") Explanation:
    • Checks if AnnualRevenue is greater than 1,000,000.
    • If true, returns "High Revenue".
    • If false, returns "Standard Revenue".
  3. Check Syntax
    • Ensure there are no errors.
  4. Save the Formula Field
    • Complete the remaining steps.

Result:

  • Accounts are categorized based on their annual revenue.

Example 4: Using Date Functions

Calculate the number of days since a case was created.

Steps:

  1. Field Details
    • ObjectCase
    • Field LabelDays Open
    • Field NameDays_Open
    • Formula Return TypeNumber
      • Decimal Places0
  2. Formula DefinitionplaintextCopy codeIF(IsClosed, ClosedDate - CreatedDate, TODAY() - CreatedDate) Explanation:
    • Checks if the case is closed using IsClosed.
    • If closed, subtracts CreatedDate from ClosedDate.
    • If not closed, subtracts CreatedDate from TODAY().
    • The result is the number of days the case has been open.
  3. Check Syntax
    • Ensure there are no errors.
  4. Save the Formula Field
    • Complete the remaining steps.

Result:

  • The Days Open field shows how many days the case has been open.

Conclusion

Formula fields are versatile tools in Salesforce that enhance data manipulation and display. By mastering formula fields, you can automate calculations, enforce data consistency, and provide valuable insights directly within your Salesforce org.

Experiment with different functions and operators to see how they can meet your organization’s needs. The Salesforce Formula Editor provides a wide range of functions categorized under MathLogicalTextDate/Time, and more.