Advanced Declarative Automation
In Salesforce
Using Formula Fields in Salesforce
Adrien Sacco
Salesforce Certified Professional
@AdrienSacco www.AdrienSacco.com
Overview
After this module, you will be able to:
Understand Formula Fields
Understand the Salesforce Formula
Language
Setup a Formula Field in Salesforce
Understanding Formula Fields
Formula Field
Calculated fields are read-only fields in the API. These fields are
defined by a formula, which is an algorithm that derives its value
from other fields, expressions, or values. You can filter on these
fields in SOQL, but you don’t replicate these fields. The length of
text calculated fields is 3900 characters or less—anything longer
is truncated.
Citation: Salesforce Developer Documentation, Object Reference, Field Types
Formula Fields
Are calculated based on a predefined algorithm, referencing other
fields, references or values in the database and performing
calculations on them
Are read-only, but can be used to filter data
Maximum of 3.900 characters, everything else is truncated
The formula’s return type allows you to format its output: Checkbox,
Currency, Date, Date/Time, Number, Percent, Text & Time.
How Formula Fields Work
Account Name Account Type Account
Text Picklist Number
Autonumber
ABC Toys Customer 2571
Ladder Constructions Customer 1484
Gotham Industries Prospect 3877
Account Record Table
How Formula Fields Work
Account Name Account Type Account
Text Picklist Number
Autonumber
Inputs
ABC Toys Customer 2571
Ladder Constructions Customer 1484
Formula
Gotham Industries Prospect 3877
Account Record Table
How Formula Fields Work
Account Name Account Type Account Account Code
Text Picklist Number Formula (Text)
Autonumber
Inputs Outputs
ABC Toys Customer 2571 CU-2571
Ladder Constructions Customer 1484 CU-1484
Gotham Industries Prospect 3877 Formula PR-3877
Calculated Values
Account Record Table
How Formula Fields Work
Account Name Account Type Account Account Code
Text Picklist Number Formula (Text)
Autonumber
Inputs Outputs
ABC Toys Customer 2571 CU-2571
Ladder
Customer 1484 CU-1484
Constructions
Gotham Industries Prospect 3877 Formula PR-3877
Calculated Values
Account Record Table
Setting Up a Formula Field
[1, 3, A, x]
1 2 3
Create a Formula Field Select the Return Type Write the Formula
Create a Formula Field on Select the proper Return Write the algorithm for
the Object you want it Type for the Formula calculating this field in
appear on, and which based on the results the Salesforce Formula
data it references should be formatted Language
Understanding the Salesforce
Formula Language
The Formula Language
Numerical & Boolean Operators
Most of the numerical & boolean operators you can find in any
language are also available in the Formula Language
Values
Values to perform calculations on are obtained by static values in the
formula, or values referenced from the database
Functions
Multiple functions are available to manipulate values, some of which
we will see next
Referencing Values
Comments
> Formula Result
/* This is a comment */
Referencing Values
Field References
> Formula Result
ABC Toys
Name /* This references a field on the object */
Referencing Values
Field References
> Formula Result
ABC Toys
Name /* This references a field on the object */
& /* Concatenate */
Referencing Values
Field References
> Formula Result
ABC Toys,
Name /* This references a field on the object */
& /* Concatenate */
“, “ /* Static String */
Referencing Values
Field References
Name /* This references a field on the object */ > Formula Result
& /* Concatenate */ ABC Toys, Adrien
“, “ /* Static text */
& /* Concatenate */
Owner.FirstName /* This references a field on a linked
object by following the Owner lookup field */
Important Functions
IF(logical_test, value_if_true, value_if_false)
> Formula Result
/* Checks whether a condition is true, and returns one
value if TRUE and another value if FALSE. */
Important Functions
IF(logical_test, value_if_true, value_if_false)
> Formula Result
/* Checks whether a condition is true, and returns one
value if TRUE and another value if FALSE. */
IF(AnnualRevenue > 1000000, /*logical_test */
Important Functions
IF(logical_test, value_if_true, value_if_false)
> Formula Result
/* Checks whether a condition is true, and returns one
value if TRUE and another value if FALSE. */
IF(AnnualRevenue > 1000000, /*logical_test */
“Big Account“, /* value_if_true */
Important Functions
IF(logical_test, value_if_true, value_if_false)
> Formula Result
Big Account
/* Checks whether a condition is true, and returns one
value if TRUE and another value if FALSE. */
IF(AnnualRevenue > 1000000, /*logical_test */
“Big Account“, /* value_if_true */
“Small Account“) /* value_if_false */
Important Functions
ISPICKVAL(picklist_field, text_literal)
> Formula Result
True (boolean)
/* Checks whether the value of a picklist field is
equal to a string literal */
ISPICKVAL(Type, “Customer”)
Formula Language Reference
To learn about the other Functions and Operators
available, you can access the up to date Formula
Language Reference on the Salesforce Help website.
DEMO: Setting up a Formula
Generating an Account Code
Account Name Account Type Account Number Account Code
Text Picklist Autonumber Formula (Text)
Inputs Outputs
ABC Toys Customer 2571 CU-2571
Ladder Constructions Customer 1484 CU-1484
Gotham Industries Prospect 3877 Formula PR-3877
Account Record Table Calculated Values
Demonstration Example
Example: Generating an account code
> Formula Result
IF(
Demonstration Example
Example: Generating an account code
> Formula Result
IF(ISPICKVAL(Type, “Customer”),
Demonstration Example
Example: Generating an account code
> Formula Result
CU-
IF(ISPICKVAL(Type, “Customer”),
“CU-“,
“PR-“)
Demonstration Example
Example: Generating an account code
> Formula Result
CU-2571
IF(ISPICKVAL(Type, “Customer”),
“CU-“,
“PR-“)
& AccountNumber
Demo Generating an Account Code:
1. Setup a new Formula Field on Account
2. Choose a Text Return Type
3. Write the proper Formula Algorithm