Get Started with Formulas
Learning Objectives
After completing this unit, you’ll be able to:
- Explain why formula fields are useful.
- Choose the correct formula return types.
- Describe the different parts of a formula editor.
- List different formula operators.
Data Insights with Formulas
You’ve got a lot of data in your organization. Your users need to access and understand this data at a glance without doing a bunch of calculations in their heads. Formulas are a fast and easy way to generate data insights. Formulas can display and leverage a great deal of new, untapped information from data that already exists on your records. Formulas are a powerful tool that you can use to embed hyperlinks, perform calculations with existing fields, or apply conditional logic to display data in different ways.
Using formulas in Salesforce is similar to creating a formula in a spreadsheet cell based on values from other columns, spreadsheets, or documents. In Salesforce, formulas use values from the current record or associated records. The formulas can be displayed or used almost anywhere you want your users to experience them, such as:
- formula fields
- validation rules
- approval processes
- custom summary formulas for reports
- flows
Let’s dig into formula fields and see what they can do.
Engage with Formula Fields
Formula fields have slightly different characteristics from standard fields. They’re automatically calculated, they’re read-only on record detail pages, and they don’t update last modified date fields. When you create a formula field, Salesforce automatically populates the new field on historical records and all new records. No need to backfill data.
Just like a standard field, a formula field has a data type, which determines the type of data the formula returns.
Formula Return Types
Data type |
Value returned |
---|---|
Checkbox |
True (checked) or false (unchecked) Example: A “Hot” checkbox that indicates whether an opportunity is hot or not |
Currency |
A currency amount, formatted with the currency symbol |
Date |
A date Example: A “Due Date” calculated by adding 10 days to another date |
Date/Time |
A date/time Example: A deadline calculated by adding a number of hours or days to another date/time |
Number |
A numeric value |
Percent |
A percent, formatted with the percent symbol |
Text |
Unformatted text Example: A field that combines the text values from two other text fields |
Time |
A time Example: An “Expiration” time calculated by adding a number of hours to another time |
Creating a Formula Field
After you select the return data type, you get to the formula editor. The formula editor has two options: Simple and Advanced. The simple editor is shown by default, but we recommend using the advanced editor. Advanced doesn’t mean you have to be an advanced formula creator, it just gives you more tools.
There are various tools available for building both simple and complex formulas.
-
Insert Field: Opens a menu to select fields from the object and related objects. Using this menu automatically generates the correct syntax for the formula to access the fields.
-
Insert Operator: Opens a list of available mathematical and logical operators.
-
Functions: Displays available formula functions that you can insert into the formula.
Functions can transform data, perform calculations, and test data more comprehensively than operators. Some functions stand alone. For example, the TODAY function returns the current date without any additional input. Other functions require additional input, called parameters or arguments.
Input parameters and arguments pass information into a function. The parameter is the variable declared in parentheses within the function. For example, in the functionLEN(text)
, the parameter istext
. The argument is the value assigned to the parameter when the function is called. Continuing the example, when callingLEN(“Hello”)
, the argument is“Hello”
. It returns a value of 5 because there are 5 characters in Hello. (The quotation marks aren’t counted because they declare Hello as text.)
While you’ll find common functions such as IF, there are Salesforce-specific functions as well, like ISPICKVAL, used when working with picklist fields within a formula.
-
Check Syntax: Identifies errors in the syntax of your formula so you can fix them before saving.
Working with Operators
Operators specify the type of calculation to perform or the order in which to perform calculations. You use different types of operators (represented by symbols) to build formulas. There are three types of operators: math, logical, and text.
Let’s jump into operators and learn more about each type.
Math Operators
Use math operator symbols to work with numbers and dates. Each symbol either specifies a type of calculation or dictates the order in which to perform calculations.
Operator |
Symbol |
Description |
---|---|---|
Add |
+ |
Calculate the sum of two values. |
Subtract |
- |
Calculate the difference between two values. |
Multiply |
* |
Multiply values. |
Divide |
/ |
Divide values. |
Exponentiation |
^ |
Raise a number to the power of the number you provide as input. |
Open and Closed Parenthesis |
() |
Evaluate the expression within parentheses first. Then evaluate other expressions using standard operator precedence. |
Logical Operators
These operators create conditional statements, to compare specified values and return either true or false.
Operator |
Symbol |
Description |
---|---|---|
Equal |
|
Evaluates whether two values are equivalent. The |
Not Equal |
|
Evaluates whether two values aren’t equivalent. The |
Less Than |
< |
Evaluates whether a value is less than the value after the |
Greater Than |
> |
Evaluates whether a value is greater than the value after the |
Less Than or Equal |
<= |
Evaluates whether a value is less than or equal to the value after the |
Greater Than or Equal |
>= |
Evaluates whether a value is greater than or equal to the value after the |
And |
&& |
Evaluates whether multiple values or expressions are all true. |
Or |
|| |
Evaluates whether at least one of multiple values or expressions is true. |
Text Operators
These operators manipulate text values.
Operator |
Symbol |
Description |
---|---|---|
Concatenate |
|
Connects two or more text values. |
Functions for Fun
Functions are system-defined formulas that return one or more values. Just as there are different types of operators, there are different types of functions. All functions are available everywhere that you can include a formula, including formula fields, validation rules, approval processes, and workflow rules, unless otherwise specified. There are a LOT of functions. Here are just a few of the different types of functions.
Date and Time Functions
Date and Time functions create or manipulate date, time, or date/time values. Here are some of them.
Function |
Syntax |
Description |
---|---|---|
DATE |
DATE(year,month,day) |
Returns a date value from the year, month, and day that you input. |
NOW |
NOW() |
Returns a date/time representing the current moment. |
TODAY |
TODAY() |
Returns the current date formatted as a date. |
DATEVALUE |
DATEVALUE(expression) |
Returns a date value for a date/time or text value. |
Logical Functions
Logical functions provide options for including logic in formulas. Here are some of them.
Function |
Syntax |
Description |
---|---|---|
AND |
AND(logical1,logical2,...) |
Determines whether multiple logical expressions are true or false. Returns TRUE if all values are true, or FALSE if any value is false. |
CASE |
CASE(expression, value1, result1, value2, result2, ..., else_result) |
Checks an |
IF |
IF(logical_test, value_if_true, value_if_false) |
Determines if a |
ISBLANK |
ISBLANK(expression) |
Determines whether an |
OR |
OR(logical1,logical2,...) |
Determines whether multiple logical expressions are true or false. Returns TRUE if any expression is true, or FALSE if all expressions are false. |
Math Functions
Math functions manipulate and calculate numbers and sometimes dates and times. Here are some of them.
Function |
Syntax |
Description |
---|---|---|
MAX |
MAX(number,number,...) |
Returns the highest number from a list of numbers. |
MIN |
MIN(number,number,...) |
Returns the lowest number from a list of numbers. |
PI |
PI() |
Returns pi. |
ROUND |
ROUND(number,num_digits) |
Returns the nearest number to a number that you specify, constraining the new number by a specified number of decimal places. |
Text Functions
Text functions manipulate texts. Here are some of them.
Function |
Syntax |
Description |
---|---|---|
BEGINS |
BEGINS(text, compare_text) |
Determines whether text begins with characters you provide as input, and returns TRUE if it does or FALSE if it doesn’t. |
BR |
BR() |
Inserts a line break in a string of text. |
CONTAINS |
CONTAINS(text, compare_text) |
Compares two arguments of text, and returns TRUE if the first argument contains the second argument or FALSE if it doesn’t. |
HYPERLINK |
HYPERLINK(url, friendly_name [, target]) |
On text that you specify, the |
Summary Functions
Summary functions are available with summary, matrix, and joined reports.
Function |
Syntax |
Description |
---|---|---|
PARENTGROUPVAL |
PARENTGROUPVAL(summary_field, grouping_level) or PARENTGROUPVAL(summary_field, parent_row_grouping, parent_column_grouping)
|
Returns the value of a specified parent grouping. A parent grouping is any level above the one that contains the formula. You can use this function only in custom summary formulas and at grouping levels (not summary levels) for reports. |
PREVGROUPVAL |
PREVGROUPVAL(summary_field, grouping_level [, increment]) |
Returns the value of a specified previous grouping (a grouping that comes before the current grouping) in the report. Choose the grouping level and increment (the number of columns or rows before the current summary). The default is 1 and the maximum is 12. You can use this function only in custom summary formulas and at grouping levels for reports (not at summary levels for reports). |
Advanced Functions
While these are the most commonly used functions, there are advanced functions that provide additional functionality. To learn more about them, see Formula Operators and Functions by Context.
Alright, alright, alright! That’s a lot of operators and functions. You’ll find that they’re pretty easy to work with. You use some of these operators to build a couple of formula fields in the next unit.