Formula Fields are an out-of-the-box Salesforce feature that enables you to manipulate (get more out of) your existing Salesforce data. Since most of us are familiar with Excel, these fields actually allow us to create simple and complex formulas based on data from a record and from its related records.

Thank you for reading this post, don't forget to subscribe!

Your custom formula fields require special attributes.

  1. Begin building a formula field the same way you create a custom field. 
  2. Select the data type for the formula. Choose the appropriate data type for your formula based on the output of your calculation. See Formula Data Types.
  3. Choose the number of decimal places for currency, number, or percent data types. This setting is ignored for currency fields in multi currency organizations. Instead, the Decimal Places for your currency setting apply. Salesforce uses the round half up tie-breaking rule for numbers in formula fields. For example, 12.345 becomes 12.35 and −12.345 becomes −12.35.
  4. Click Next.
  5. Build your formula. Formula fields can contain up to 3,900 characters, including spaces, return characters, and comments. If your formula requires more characters, create separate formula fields and reference them in another formula field. The maximum number of displayed characters after an evaluation of a formula expression is 1,300
    1. If you are building a formula in the Advanced Formula tab or for approvals or rules, such as workflow, validation, assignment, auto-response, or escalation, click Insert Field, choose a field, and click Insert. To create a basic formula that passes specific Salesforce data, select the Simple Formula tab, choose the field type in the Select Field Type drop-down list, and choose one of the fields listed in the Insert Field drop-down list. Build cross-object formulas to span to related objects and reference merge fields on those objects.
    2. To insert an operator, choose the appropriate operator icon from the Insert Operator drop-down list.
    3. Optionally, click the Advanced Formula tab to use functions and view other operators and merge fields. Functions are prebuilt formulas that you can customize with your input parameters.
    4. To insert a function, double-click its name in the list, or select it and click Insert Selected Function. To filter the list of functions, choose a category from the Functions drop-down list. Select a function and click Help on this function to view a description and examples of formulas using that function.
    5. Consider adding comments to your formula, especially if it is complicated. Comments must begin with a forward slash followed by an asterisk (/*), and conclude with an asterisk followed by a forward slash (*/).
      Comments are useful for explaining specific parts of a formula to anyone viewing the formula definition. For example:


AND( 

/*competitor field is required, check to see if field is empty */

LEN(Competitor__c) = 0, 

/* rule only enforced for ABCD record types */

RecordType.Name = “ABCD Value”,

/* checking for any closed status, allows for additional closed picklist values in the future */

CONTAINS(TEXT(StageName), “Closed”) 

)

  1. To check your formula for errors, click Check Syntax.
  2. Optionally, enter a description of the formula in the Description box.
  3. If your formula references any number, currency, or percent fields, choose an option for handling blank fields. To give any blank fields a zero value, choose Treat blank fields as zeros. To leave these fields blank, choose Treat blank fields as blanks.
  4. Click Next.
  5. Set the field-level security to determine whether the field should be visible for specific profiles, and click Next.
  6. Choose the page layouts that should display the field. The field is added as the last field in the first two-column section on the page layout. For user custom fields, the field is automatically added to the bottom of the user detail page.
  7. Click Save to finish or Save & New to create more custom fields.
Salesforce Formula Fields

Math Operators

OPERATORDESCRIPTION
+ (Add)Calculates the sum of two values.
– (Subtract)Calculates the difference of two values.
* (Multiply)Multiplies its values.
/ (Divide)Divides its values.
^ (Exponentiation)Raises a number to the power of a specified number.
() (Open Parenthesis and Closed Parenthesis)Specifies that the expressions within the open parenthesis and close parenthesis are evaluated first. All other expressions are evaluated using standard operator precedence.

Logical Operators

OPERATORDESCRIPTION
= and == (Equal)Evaluates if two values are equivalent. The = and == operators are interchangeable.
<> and != (Not Equal)Evaluates if two values aren’t equivalent.
< (Less Than)Evaluates if a value is less than the value that follows this symbol.
> (Greater Than)Evaluates if a value is greater than the value that follows this symbol.
<= (Less Than or Equal)Evaluates if a value is less than or equal to the value that follows this symbol.
>= (Greater Than or Equal)Evaluates if a value is greater than or equal to the value that follows this symbol.
&& (And)Evaluates if two values or expressions are both true. Use this operator as an alternative to the logical function AND.
|| (Or)Evaluates if at least one of multiple values or expressions is true. Use this operator as an alternative to the logical function OR.

Text Operators

OPERATORDESCRIPTION
& and + (Concatenate)Connects two or more strings.

Date and Time Functions

FUNCTIONDESCRIPTION
ADDMONTHSReturns the date that is the indicated number of months before or after a specified date. If the specified date is the last day of the month, the resulting date is the last day of the resulting month. Otherwise, the result has the same date component as the specified date.
DATEReturns a date value from the year, month, and day values that you enter. Salesforce displays an error on the detail page if the value of the DATE function in a formula field is an invalid date, such as February 29 in a non-leap year.
DATEVALUEReturns a date value for a date/time or text expression.
DATETIMEVALUEReturns a year, month, day, and GMT time value.
DAYReturns a day of the month in the form of a number from 1 through 31.
DAYOFYEARReturns the day of the calendar year in the form of a number from 1 through 366.
FORMATDURATIONFormats the number of seconds with optional days, or the difference between times or dateTimes as HH:MI:SS.
HOURReturns the local time hour value without the date in the form of a number from 1 through 24.
ISOWEEKReturns the ISO 8601-week number, from 1 through 53, for the given date, ensuring that the first week starts on a Monday.
ISOYEARReturns the ISO 8601 week-numbering year in 4 digits for the given date, ensuring that the first day is a Monday.
MILLISECONDReturns a milliseconds value in the form of a number from 0 through 999.
MINUTEReturns a minute value in the form of a number from 0 through 60.
MONTHReturns the month, a number between 1 and 12 (December) in number format of a given date.
NOWReturns a date/time representing the current moment.
SECONDReturns a seconds value in the form of a number from 0 through 60.
TIMENOWReturns a time value in GMT representing the current moment. Use this function instead of the NOW function if you only want to track time, without a date.
TIMEVALUEReturns the time value without the date, such as business hours.
TODAYReturns the current date as a date data type.
UNIXTIMESTAMPReturns the number of seconds since 1 Jan 1970 for the given date, or number of seconds in the day for a time.
WEEKDAYReturns the day of the week for the given date, using 1 for Sunday, 2 for Monday, through 7 for Saturday.
YEARReturns the four-digit year in number format of a given date.

Logical Functions

FUNCTIONDESCRIPTION
ANDReturns a TRUE response if all values are true, and returns a FALSE response if one or more values are false.
BLANKVALUEDetermines if an expression has a value, and returns a substitute expression if it doesn’t. If the expression has a value, it returns the value of the expression.
CASEChecks a given expression against a series of values. If the expression is equal to a value, it returns the corresponding result. If it isn’t equal to any values, it returns the else_result.
IFDetermines if expressions are true or false. Returns a given value if true and another value if false.
ISBLANKDetermines if an expression has a value, and returns TRUE if it doesn’t. If it contains a value, this function returns FALSE.
ISCLONEChecks if the record is a clone of another record, and returns TRUE if one item is a clone. Otherwise, returns FALSE.
ISNEWChecks if the formula is running during the creation of a new record, and returns TRUE if it is. If an existing record is being updated, this function returns FALSE.
ISNULLDetermines if an expression is null (blank), and returns TRUE if it is. If it contains a value, this function returns FALSE.You must use ISBLANK instead of ISNULL in new formulas. ISBLANK has the same functionality as ISNULL, but also supports text fields. Salesforce continues to support ISNULL, so you don’t change any existing formulas.
ISNUMBERDetermines if a text value is a number, and returns TRUE if it is. Otherwise, it returns FALSE.
NOTReturns FALSE for TRUE and TRUE for FALSE.
NULLVALUEDetermines if an expression is null (blank) and returns a substitute expression if it is. If the expression isn’t blank, returns the value of the expression.You must use BLANKVALUE instead of NULLVALUE in new formulas. BLANKVALUE has the same functionality as NULLVALUE, but it also supports text fields. Salesforce continues to support NULLVALUE, so changing the existing formulas isn’t necessary.
ORDetermines if expressions are true or false. Returns TRUE if any expression is true, and returns FALSE if all expressions are false.
PRIORVALUEReturns the previous value of a field.

Math Functions

FUNCTIONDESCRIPTION
ABSCalculates the absolute value of a number. The absolute value of a number is the number without its positive or negative sign.
ACOSReturns the arc cosign of the number in radians, if the given number is between -1 and 1. Otherwise, returns NULL.
ASINReturns the arc sine of the number in radians, if the given number is between -1 and 1. Otherwise, returns NULL.
ATANReturns the arc tangent of the number in radians.
ATAN2Returns the arc tangent of the quotient of y and x in radians.
CEILINGRounds a number up to the nearest integer, away from zero if negative.
CHRReturns a string with the first character’s code point as the given number.
COSReturns the cosine of the number in radians, if the given number is between -1 and 1. Otherwise, returns NULL.
DISTANCECalculates the distance between two locations in miles or kilometers.
EXPReturns a value for e raised to the power of a number that you specify.
FLOORReturns a number rounded down to the nearest integer, towards zero if negative.
FROMUNIXTIMEReturns the datetime that represents the given number as the seconds elapsed since 1 Jan 1970.
GEOLOCATIONReturns a geolocation based on the provided latitude and longitude. Must be used with the DISTANCE function.
LNReturns the natural logarithm of a specified number. Natural logarithms are based on the constant e value of 2.71828182845904.
LOGReturns the base 10 logarithm of a number.
MAXReturns the highest number from a list of numbers.
MCEILINGRounds a number up to the nearest integer, towards zero if negative.
MFLOORRounds a number down to the nearest integer, away from zero if negative.
MINReturns the lowest number from a list of numbers.
MODReturns a remainder after a number is divided by a specified divisor.
PIReturns pi.
PICKLISTCOUNTReturns the number of selected values in a multi-select picklist.
ROUNDReturns the nearest number to a number that you specify, constraining the new number by a specified number of digits.
SINReturns the sine of the number, where the number is given in radians.
SQRTReturns the positive square root of a given number.
TANReturns the tangent of the number, where the number is given in radians.
TRUNCTruncates a number to a specified number of digits.

Text Functions

FUNCTIONDESCRIPTION
ASCIIReturns the first character’s code point from the given string as a number.
BEGINSDetermines if text begins with specific characters. Returns TRUE if it does, and returns FALSE if it doesn’t.
BRInserts a line break in a string of text.
CASESAFEIDConverts a 15-character ID to a case-insensitive 18-character ID.
CONTAINSCompares two arguments of text, and returns TRUE if the first argument contains the second argument. If not, returns FALSE.
FINDReturns the position of a string within a string of text represented as a number.
GETSESSIONIDReturns the user’s session ID.
HTMLENCODEEncodes text and merge field values for use in HTML by replacing characters that are reserved in HTML, such as the greater-than sign (>), with HTML entity equivalents, such as &gt;.
HYPERLINKCreates a link to a URL specified that is linkable from the text specified.
IMAGEInserts an image with alternate text and height and width specifications.
INCLUDESDetermines if any value selected in a multi-select picklist field equals a text literal that you specify.
INITCAPReturns the text as lowercase with the first character of each word in uppercase.
ISPICKVALDetermines if the value of a picklist field is equal to a text literal that you specify.
JSENCODEEncodes text and merge field values for use in JavaScript by inserting escape characters, such as a backslash (\), before unsafe JavaScript characters, such as the apostrophe (‘).
JSINHTMLENCODEncodes text and merge field values for use in JavaScript inside HTML tags by replacing characters that are reserved in HTML with HTML entity equivalents and inserting escape characters before unsafe JavaScript characters. JSINHTMLENCODE(someValue) is a convenience function that is equivalent to JSENCODE(HTMLENCODE((someValue)). That is, JSINHTMLENCODE first encodes someValue with HTMLENCODE, and then encodes the result with JSENCODE.
LEFTReturns the specified number of characters from the beginning of a text string.
LENReturns the number of characters in a specified text string.
LOWERConverts all letters in the specified text string to lowercase. Any characters that aren’t letters are unaffected by this function. Locale rules are applied if a locale is provided.
LPADInserts characters that you specify to the left-side of a text string.
MIDReturns the specified number of characters from the middle of a text string given the starting position.
REVERSEReturns the characters of a source text string in reverse order.
RIGHTReturns the specified number of characters from the end of a text string.
RPADInserts characters that you specify to the right-side of a text string.
SUBSTITUTESubstitutes new text for old text in a text string.
TEXTConverts a percent, number, date, date/time, or currency type field into text anywhere formulas are used. Also, converts picklist values to text in approval rules, approval step rules, workflow rules, escalation rules, assignment rules, auto-response rules, validation rules, formula fields, field updates, and custom buttons and links.
TRIMRemoves the spaces and tabs from the beginning and end of a text string.
UPPERConverts all letters in the specified text string to uppercase. Any characters that aren’t letters are unaffected by this function. Locale rules are applied if a locale is provided.
URLENCODEEncodes text and merge field values for use in URLs by replacing characters that are illegal in URLs, such as blank spaces, with the code that represent those characters as defined in RFC 3986, Uniform Resource Identifier (URI): Generic Syntax. For example, blank spaces are replaced with %20, and exclamation points are replaced with %21.
VALUEConverts a text string to a number.

Summary Functions

These functions are available with summary, matrix, and joined reports.

FUNCTIONDESCRIPTION
PARENTGROUPVALThis function returns the value of a specified parent grouping. A “parent” grouping is any level above the one containing the formula. You can use this function only in custom summary formulas and at grouping levels for reports, but not at summary levels.
PREVGROUPVALThis function returns the value of a specified previous grouping. A “previous” grouping is one that comes before the current grouping in the report. Choose the grouping level and increment. The increment is the number of columns or rows before the current summary. The default is 1, the maximum is 12. You can use this function only in custom summary formulas and at grouping levels for reports, but not at summary levels.

Advanced Functions

FUNCTIONDESCRIPTION
CURRENCYRATEReturns the conversion rate to the corporate currency for the given currency ISO code. If the currency is invalid, returns 1.0.
GETRECORDIDSReturns an array of strings in the form of record IDs for the selected records in a list, such as a list view or related list.
IMAGEPROXYURLSecurely retrieves external images, and prevents unauthorized requests for user credentials.
INCLUDEReturns content from an s-control snippet. Use this function to reuse common code in many s-controls.
ISCHANGEDCompares the value of a field to the previous value, and returns TRUE if the values are different. If the values are the same, this function returns FALSE.
JUNCTIONIDLISTReturns a JunctionIDList based on the provided IDs.
LINKTOReturns a relative URL in the form of a link (href and anchor tags) for a custom s-control or Salesforce page.
PREDICTReturns an Einstein Discovery prediction for a record based on the specified record ID or for a list of fields and their values.
REGEXCompares a text field to a regular expression, and returns TRUE if there’s a match. Otherwise, it returns FALSE. A regular expression is a string used to describe a format of a string according to certain syntax rules.
REQUIRESCRIPTReturns a script tag with source for a URL that you specify. Use this function when referencing the Lightning Platform AJAX Toolkit or other JavaScript toolkits.
URLFORReturns a relative URL for an action, s-control, Visualforce page, or a file in a static resource archive in a Visualforce page.
VLOOKUPReturns a value by looking up a related value on a custom object similar to the VLOOKUP() Excel function.
  • All Formula Operators and Functions
    Use operators and functions when building formulas. All functions are available everywhere that you can include a formula such as formula fields, validation rules, approval processes, and workflow rules, unless otherwise specified.
Related Posts
Salesforce OEM AppExchange
Salesforce OEM AppExchange

Expanding its reach beyond CRM, Salesforce.com has launched a new service called AppExchange OEM Edition, aimed at non-CRM service providers. Read more

The Salesforce Story
The Salesforce Story

In Marc Benioff's own words How did salesforce.com grow from a start up in a rented apartment into the world's Read more

Salesforce Jigsaw
Salesforce Jigsaw

Salesforce.com, a prominent figure in cloud computing, has finalized a deal to acquire Jigsaw, a wiki-style business contact database, for Read more

Health Cloud Brings Healthcare Transformation
Health Cloud Brings Healthcare Transformation

Following swiftly after last week's successful launch of Financial Services Cloud, Salesforce has announced the second installment in its series Read more