Sub topic 3
Operators and everyday functions
This chapter covers the operators and functions you will use in most formula fields. It assumes you can open the formula editor in Object Manager and that you know the return types from the previous chapter (Text, Number, Currency, Percent, Date, Date/Time, Checkbox). Branching with IF and CASE and null handling get their own chapters, so they appear here only where they are needed.
Operators
An operator combines values into a new value. Salesforce formulas use these groups.
| Group | Operators | Example | Result type | Column | Column |
|---|---|---|---|---|---|
| Arithmetic | `+` `-` `*` `/` `^` | `Amount * 0.1` | Number or Currency | ||
| Comparison | `=` `==` `!=` `<>` `<` `>` `<=` `>=` | `Amount >= 10000` | Checkbox (true/false) | ||
| Logical | `&&` `\ | \ | ` `!` (or `AND`, `OR`, `NOT`) | `Amount > 0 && IsClosed` | Checkbox |
| Text | `&` (also `+`) | `"Deal: " & Name` | Text |
Some details cause most beginner errors:
=and==are the same comparison. A single=never assigns anything, because formulas have no variables./always gives a decimal result.7 / 2is3.5, not3. Round it yourself if you need a whole number.^raises to a power, so2 ^ 3is8.- Both sides of
&must be text. Number, date, and picklist values must be converted first withTEXT().
Precedence
Salesforce evaluates parentheses first, then ^, then * and /, then + and -, then comparisons, then &&, and || last. You do not need to memorize this. Add parentheses whenever a reader could doubt the order.
(Amount - Discount__c) * 0.2 + 50Without the parentheses, Discount__c * 0.2 would be computed first, and the result would be wrong.
Text functions
These functions come up constantly. Each takes text as input unless noted.
| Function | What it does | Example | Returns |
|---|---|---|---|
| `LEN(text)` | Counts characters | `LEN("Acme")` | `4` |
| `LEFT(text, n)` / `RIGHT(text, n)` | First or last n characters | `LEFT(Name, 3)` | Text |
| `MID(text, start, n)` | n characters from a 1-based position | `MID(Name, 5, 3)` | Text |
| `UPPER` / `LOWER` | Changes case | `LOWER(Name)` | Text |
| `TRIM(text)` | Removes leading and trailing spaces | `TRIM(Name)` | Text |
| `CONTAINS(text, part)` | True if `part` appears in `text` | `CONTAINS(Name, "Renewal")` | Checkbox |
| `BEGINS(text, part)` | True if text starts with `part` | `BEGINS(Name, "FY")` | Checkbox |
| `SUBSTITUTE(text, old, new)` | Replaces every match | `SUBSTITUTE(Name, " ", "_")` | Text |
| `FIND(part, text)` | 1-based position of a match, or `0` if none | `FIND("-", Name)` | Number |
| `TEXT(value)` | Converts a number, date, or picklist to text | `TEXT(CloseDate)` | Text |
CONTAINS and FIND are case-sensitive in most cases. For a case-insensitive check, lower-case both sides:
CONTAINS(LOWER(Name), "renewal")Building a label with TEXT and &
The usual pattern joins text pieces with & and converts non-text values with TEXT() first.
Account.Name & " - " & TEXT(StageName) & " (" & TEXT(Amount) & ")"Two limits apply to text formulas. A Text formula returns at most 1,300 characters. TEXT() on a number uses the user's locale and does not apply fixed decimal places. Use ROUND first if you want a specific precision.
Math functions
| Function | What it does | Example | Returns |
|---|---|---|---|
| `ROUND(n, places)` | Rounds to the given decimal places | `ROUND(2.567, 2)` | `2.57` |
| `CEILING(n)` / `FLOOR(n)` | Rounds up or down to an integer | `CEILING(2.1)` | `3` |
| `ABS(n)` | Absolute value | `ABS(-5)` | `5` |
| `MOD(n, divisor)` | Remainder | `MOD(10, 3)` | `1` |
| `MAX(a, b, ...)` / `MIN(a, b, ...)` | Largest or smallest of the arguments | `MAX(Amount, 0)` | Number |
| `SQRT(n)` | Square root | `SQRT(16)` | `4` |
ROUND rounds halves away from zero, so ROUND(2.5, 0) is 3 and ROUND(-2.5, 0) is -3. The formula field's own decimal-places setting only changes how the value displays. ROUND changes the value other formulas see.
A weighted pipeline value shows ROUND and arithmetic together. Probability is a Percent field, and 25% is used in math as 0.25.
ROUND(Amount * Probability, 2)Set the formula's return type to Currency. If you choose Number, the value loses currency formatting.
Logical helpers
Two functions from the logic family appear in almost every formula, so you need them now.
IF(test, value_if_true, value_if_false)returns one of two values. Both branches must have the same type.ISPICKVAL(field, "Label")is the only way to compare a picklist to a literal. WritingStageName = "Closed Won"fails to save with a type error, because a picklist is not text.TEXT(StageName) = "Closed Won"also works and reads more naturally when you compare several values.
IF(ISPICKVAL(StageName, "Closed Won"), Amount, 0)Picklist comparisons use the stored value's label text. If an admin renames a picklist value, formulas that compare against the old label stop matching, and Salesforce does not warn you.
Common errors
- "Field X is a picklist field. Picklist fields are only supported in certain functions." You used a picklist directly. Wrap it in
ISPICKVALorTEXT. - "Incorrect parameter type for operator '&'." One side is a number, date, or picklist. Wrap it in
TEXT(). - "Formula result is data type (Text), incompatible with expected data type (Number)." The return type you selected does not match the expression. Change the return type or the expression.
- Unexpected results in division. Remember
/returns decimals. A result of0.3333333in a Number field with 2 decimal places displays as0.33, but the stored value keeps full precision. - Wrong operator order. If a result is off by a factor, add parentheses and retest.
Putting it together
This formula returns a short account-and-deal tag. It uses a comparison, a logical operator, three text functions, and a math function.
IF(
Amount >= 10000 && NOT(ISPICKVAL(StageName, "Closed Lost")),
UPPER(LEFT(Account.Name, 3)) & "-" & TEXT(ROUND(Amount / 1000, 0)) & "K",
"SMALL"
)For an open deal of 25,400 on an account named "Acme Corp", the result is ACM-25K. The formula takes the first three letters of the account name, upper-cases them, joins a hyphen, rounds the amount in thousands, and adds a K. For a closed-lost deal, or any amount under 10,000, it returns SMALL. Set the return type to Text.
Exercise
Write a Text formula for Opportunity that returns the first 10 characters of the opportunity name in lower case, followed by " | " and the stage label. Why can't you write Name & " | " & StageName?
Show answer
Use LOWER(LEFT(Name, 10)) & " | " & TEXT(StageName). StageName is a picklist, and the & operator needs text on both sides, so the picklist must be converted with TEXT() first. Without it, the formula fails to save with a type error.
Exercise
A formula Amount / 2 + 10 * 3 returns 30 plus half the amount. A teammate expected it to return half of Amount + 10, then times 3. Rewrite it with parentheses to match the teammate's expectation.
Show answer
(Amount + 10) / 2 * 3. Multiplication and division run before addition, so without parentheses 10 * 3 is computed first. The parentheses force Amount + 10 to be added before the division and multiplication.