Elements of a Formula

Learn the elements you can combine to build a formula: literal values, field references, functions, operators, and comments. Understand what each element does and see an example of how to enter it.

How a Formula Is Built

Use any or all of these elements to build a formula. This example formula combines all elements to calculate 2% of an amount and round the result to two decimal places:

/* 2% of Amount, rounded to two decimal places */
ROUND( ( Amount*0.02 ), 2 )

This example uses:

  • A function, ROUND, which returns a number rounded to a specified number of decimal places.
  • A field reference, Amount.
  • A literal value, 0.02.

  • An operator, *, which multiplies the value of the Amount field by the literal value 0.02.

  • Another operator, parentheses ( ), which groups Amount*0.02 as the first argument to ROUND.

  • A second literal value, 2, which is the input that tells ROUND how many decimal places to return.
  • A comment that describes the formula. Comments start with /* and end with */. They’re ignored when the formula runs.

Literal Value

A literal value is a number or text string that you enter directly in a formula. The value is used exactly as entered, without calculating or changing it.

  • Enter a number directly, as plain digits without commas or currency symbols. Use the decimal form for percentages—for example, enter 0.02 for 2%. To represent $850,000, enter 850000, not 850,000 or $850000.
  • Enter text by enclosing it in quotation marks. For example, this formula uses the IsWon checkbox on an opportunity and returns a text literal:
    IF( IsWon, "Won", "Not Won" )

    It returns the text “Won” when the opportunity is won. Otherwise it returns “Not Won”.

    To include a quotation mark or backslash within the text, precede it with a backslash. For an example, see Tips for Working with Text in Formulas.

Field Reference

A field reference (a merge field) uses the value of another standard or custom field in your formula. Use these syntax forms:

  • Standard field: field_name (for example, Amount)
  • Custom field: field_name__C (for example, Discount_Percent__c)
  • Field on a related object (standard relationship): RelationshipName.field_name (for example, Account.Name)
  • Field on a related object (custom relationship): object_name__r.field_name or object_name__r.field_name__c (for example, Position__r.title__c for a custom field)
Note
Note For fields on related objects, the relationship name is usually, but not always, the object name. For the full rules on relationship names, see Merge Fields for Cross-Object Formulas.

You can also reference a value from a custom metadata type. That’s configuration data stored separately from the record, not a field on the object. Use the $CustomMetadata global variable, and identify the type, the record, and the field. Use this syntax, replacing each token with your own API names:

$CustomMetadata.CustomMetadataTypeAPIName.RecordAPIName.FieldAPIName

In this example, AnnualRevenue__mdt is the custom metadata type, Annual_Revenue is the record’s API name, and Maximum_Revenue__c is the field:

$CustomMetadata.AnnualRevenue__mdt.Annual_Revenue.Maximum_Revenue__c
Note
Note Referencing custom metadata in formula fields is available in Enterprise, Performance, Unlimited, and Developer Editions.

To insert a merge field without typing it, use the Insert Field button or the dropdown list.

Function

A function is a system-defined formula that returns a value or values, and can require input from you. Salesforce provides many functions. These functions show how input works:

  • TODAY() requires no input and returns the current date.
  • TEXT( value ) requires input, such as a percent, number, or currency value, and returns text.

    For example, TEXT(ExpectedRevenue) converts a currency amount such as $200,000 to the text 200000, without the dollar sign or comma.

For a complete list of functions, see Formula Operators and Functions by Context.

Operator

An operator is a symbol that specifies the type of calculation to perform, or the order in which to perform calculations. For example:

  • The + symbol adds two values.
  • Parentheses ( ) specify which expressions to evaluate first.

Comparison operators test whether a condition is true. They include:

  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)
  • = (equal to)
  • <> (not equal to)

This example uses < to compare a currency field with a number:

AnnualRevenue < 850000

AnnualRevenue < 850000 is TRUE when an account’s annual revenue is under $850,000.

For the complete list of operators, see Formula Operators and Functions by Context.

Comment

A comment is an annotation within a formula. It begins with a forward slash and asterisk (/*) and ends with an asterisk and forward slash (*/), as shown in this example:

/*This is a formula comment*/

Comments document specific parts of a formula for anyone viewing its definition. Salesforce ignores comments when it processes a formula.

This example uses comments to document each part of a multi-part formula. The field names, record type name, and picklist text are for illustration. Replace them with values from your org.

AND( 
/*competitor field is required, check to see if field is empty */
LEN( Competitor__c ) = 0, 
/* rule only enforced for New Business record types */
RecordType.Name = "New Business",
/* checking for any closed status, allows for additional closed picklist values in the future */
CONTAINS( TEXT( StageName ), "Closed" ) 
)

You can also comment out sections of a formula to debug and check the syntax while locating errors.

Keep these considerations in mind:

  • Nesting comments causes a syntax error. For example, you can’t save a formula that contains this string:
    /* /* comment */ */
  • Commenting out an entire formula causes a syntax error.
  • Comments count against the character and byte size limits in formulas.