Here’s a basic guide with examples for some commonly used formula elements in Salesforce:
Basic Syntax
- Field References:
- Reference a field value:
ObjectName__c.Field__c
- Reference a field value:
- Mathematical Operators:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Addition:
- Comparison Operators:
- Equal to:
==
- Not equal to:
!=
- Greater than:
>
- Less than:
<
- Greater than or equal to:
>=
- Less than or equal to:
<=
- Equal to:
Text Functions
- Concatenate:
- Combine text fields:
FirstName & " " & LastName
- Combine text fields:
- Text Length:
- Get the length of a text field:
LEN(TextField__c)
- Get the length of a text field:
- Substring:
- Extract a portion of a text:
MID(TextField__c, 2, 5)
(starting at position 2 and taking 5 characters)
- Extract a portion of a text:
Date and Time Functions
- Today’s Date:
TODAY()
- Now:
NOW()
- Date Difference:
- Calculate the difference between two dates:
CloseDate - CreatedDate
- Calculate the difference between two dates:
Logical Functions
- IF Statement:
IF(Condition, ValueIfTrue, ValueIfFalse)
- CASE Function:
CASE(FieldValue, "Value1", Result1, "Value2", Result2, DefaultResult)
Advanced Functions
- ISBLANK:
- Check if a field is blank:
ISBLANK(Field__c)
- Check if a field is blank:
- ISNUMBER:
- Check if a field is a number:
ISNUMBER(Field__c)
- Check if a field is a number:
- HYPERLINK:
- Create a hyperlink:
HYPERLINK("http://www.example.com", "Link Text")
- Create a hyperlink:
- TEXT:
- Convert a number to text:
TEXT(NumberField__c)
- Convert a number to text:
Aggregate Functions
- SUM:
- Calculate the sum of a numeric field in a related list:
SUM(relatedListName, Field__c)
- Calculate the sum of a numeric field in a related list:
- AVG:
- Calculate the average of a numeric field in a related list:
AVG(relatedListName, Field__c)
- Calculate the average of a numeric field in a related list:
Examples
- Formula Field Example:
- Concatenate first and last name:
FirstName & " " & LastName
- Concatenate first and last name:
- Validation Rule Example:
- Allow saving only if the amount is greater than $100:
Amount > 100
- Allow saving only if the amount is greater than $100:
- Workflow Rule Example:
- Set a field value based on a condition:
IF(StageName = "Closed", "Completed", "In Progress")
- Set a field value based on a condition:
Remember, Salesforce formulas can be complex and vary depending on the use case (Validation Rules, Workflow Rules, Formula Fields, etc.). Always refer to the Salesforce documentation for the most up-to-date and detailed information on formula functions and syntax.