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.
- 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:
- Field Details
- Field Label:
Age
- Field Name:
Age
- Formula Return Type: Number
- Decimal Places:
0
- Decimal Places:
- Field Label:
- Formula DefinitionplaintextCopy code
FLOOR( ( TODAY() - Birthdate ) / 365.25 )
Explanation:TODAY()
returns the current date.Birthdate
is the field containing the contact’s birthdate.- Subtracting
Birthdate
fromTODAY()
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.
- Check Syntax
- Ensure there are no errors.
- 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:
- Field Details
- Field Label:
Full Name
- Field Name:
Full_Name
- Formula Return Type: Text
- Field Label:
- Formula DefinitionplaintextCopy code
FirstName & " " & LastName
Explanation:FirstName
andLastName
are standard fields.- The
&
operator concatenates strings. " "
adds a space between the first and last names.
- Check Syntax
- Ensure there are no errors.
- 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:
- Field Details
- Object:
Account
- Field Label:
Revenue Category
- Field Name:
Revenue_Category
- Formula Return Type: Text
- Object:
- Formula DefinitionplaintextCopy code
IF(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"
.
- Checks if
- Check Syntax
- Ensure there are no errors.
- 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:
- Field Details
- Object:
Case
- Field Label:
Days Open
- Field Name:
Days_Open
- Formula Return Type: Number
- Decimal Places:
0
- Decimal Places:
- Object:
- Formula DefinitionplaintextCopy code
IF(IsClosed, ClosedDate - CreatedDate, TODAY() - CreatedDate)
Explanation:- Checks if the case is closed using
IsClosed
. - If closed, subtracts
CreatedDate
fromClosedDate
. - If not closed, subtracts
CreatedDate
fromTODAY()
. - The result is the number of days the case has been open.
- Checks if the case is closed using
- Check Syntax
- Ensure there are no errors.
- 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 Math, Logical, Text, Date/Time, and more.