Sub topic 5
Handling blanks and avoiding divide-by-zero
This chapter assumes you can write a basic formula field, know the return types (Number, Text, Date, and so on), and know that IF and CASE exist. It covers what a formula does when an input is empty and how to stop it from returning an error.
Why blanks cause trouble
A field with no value is blank (also called null). Blank is not the same as zero or an empty string, and formulas treat them differently depending on settings and on the function you use.
Two problems come up most often:
- A blank number or date makes the whole result blank or wrong, when you wanted a sensible default.
- A division by zero returns an error. The field shows
#Error!on the record and in reports.
The blank-field treatment setting
When you save a formula field, Salesforce asks how to handle blank number inputs:
- Treat blank fields as zeroes: an empty number is read as 0 in the math.
- Treat blank fields as blanks: an empty number stays blank, and the calculation can return a blank result.
Choose deliberately. A blank Discount__c may mean "no discount", so zero is right. A blank Quantity__c may mean "not entered yet", so a blank result is more honest.
The setting only affects number-style inputs. Text, picklist, and lookup fields need explicit checks.
Testing for blank: ISBLANK and BLANKVALUE
ISBLANK(expression) returns true when the value is null or an empty string. Use it in a condition. BLANKVALUE(expression, substitute) returns the substitute when the expression is blank, and the expression otherwise. Use it to supply a default inline.
IF(BLANKVALUE(Amount, 0) > 100000, "Large",
IF(BLANKVALUE(Amount, 0) > 10000, "Medium", "Small"))Here an empty Amount becomes 0 and lands in "Small". Without BLANKVALUE, a blank Amount could give a blank or unexpected result, depending on the blank-field setting.
NULLVALUE does the same job as BLANKVALUE but only tests for null, so it does not catch empty text. Prefer BLANKVALUE and ISBLANK in new formulas. ISNULL is the older equivalent and is not recommended for text fields.
| Function | Returns | Typical use |
|---|---|---|
| `ISBLANK(x)` | true or false | Branch on whether a value exists |
| `BLANKVALUE(x, d)` | `x`, or `d` if blank | Supply a default inside math or text |
| `NULLVALUE(x, d)` | `x`, or `d` if null | Older form; does not treat empty text as blank |
Blanks in different field types
Each data type needs a slightly different check.
- Number, Currency, Percent: use
ISBLANK(Field__c)orBLANKVALUE(Field__c, 0). - Text:
ISBLANK(Name)also catches an empty string. - Date and Date/Time: always test before doing date math, for example
IF(ISBLANK(CloseDate), NULL, CloseDate - TODAY()). - Picklist:
ISBLANKdoes not accept a picklist directly. UseISBLANK(TEXT(Field__c))orISPICKVAL(Field__c, ""). - Checkbox: never blank. It is always true or false, so no check is needed.
- Lookup fields: test the relationship's ID, for example
ISBLANK(AccountId), before traversing it.
IF(ISBLANK(CloseDate), NULL, CloseDate - TODAY())A formula that returns NULL must still match its declared return type. This one is valid as a Number formula, and the field is simply empty when CloseDate is missing.
Safe division
Dividing by zero fails. The standard defense is to test the denominator first.
IF(Line_Item_Count__c = 0, 0, Total_Amount__c / Line_Item_Count__c)This formula, for an average per line item, returns 0 when there are no line items. You may prefer NULL so reports show an empty cell instead of a misleading 0:
IF(Line_Item_Count__c = 0, NULL, Total_Amount__c / Line_Item_Count__c)Whether to return 0 or NULL is a business decision. An average of 0 looks like real data. A blank says "not calculable". Pick one and use it consistently across your org.
If the denominator can also be blank, guard against both cases:
IF(BLANKVALUE(Line_Item_Count__c, 0) = 0, NULL,
Total_Amount__c / Line_Item_Count__c)Salesforce also provides a DIVIDE-style safeguard only in some other tools, not in standard formulas, so the explicit IF is the pattern to learn.
Decision flow
Blanks and roll-up summary fields
Roll-up summary fields interact with these rules. With no child records, or no children that match the filter, a COUNT or SUM roll-up returns 0. MIN and MAX return blank, because there is no value to report. A formula that reads a MIN or MAX roll-up should therefore treat blanks as possible.
The ratio formula shown above is a common use. Line_Item_Count__c is a COUNT roll-up that is 0 for a new invoice, so the guard is required, not optional.
Common mistakes
- Comparing to an empty string:
Name = ""is less reliable thanISBLANK(Name). - Using ISBLANK on a picklist directly: wrap it with
TEXT()first. - Forgetting date math on blanks: subtracting from a blank date gives a blank or a misleading value.
- Returning "0" for a Number formula's text branch: every branch of an
IFmust return the same type. Use0, not"0", in a Number formula. - Treating zero as "no data": a real value of 0 and a blank are different, so test with
ISBLANK, not= 0, when the distinction matters. - Relying on the blank-field setting alone: it is a default, not a guard. Anyone reading the formula cannot see it, so explicit
BLANKVALUEcalls document your intent.
Testing your guards
Create records that exercise each path: all inputs filled, each input blank in turn, and a zero denominator. Check the formula result on the record page, and use the formula editor's syntax check for type errors. Also confirm the result in a report, where an #Error! is easiest to spot.
Exercise
An Opportunity formula field (Number) should return Amount divided by NumberofSeats_c. NumberofSeats_c can be blank or 0. Write a formula that returns blank when seats are missing or zero, and explain why a simple AND check is not enough.
Show answer
IF(BLANKVALUE(NumberofSeats_c, 0) = 0, NULL, Amount / NumberofSeatsc). BLANKVALUE turns a blank into 0, so one comparison covers both cases, and the division sits only in the false branch of the outer IF. An AND check such as AND(NumberofSeatsc <> 0, Amount / NumberofSeats_c > 1) can evaluate the division even when the first argument is false, which can still produce an error.
Exercise
A Number formula must add 10 to a roll-up field MaxDiscount_c, which is a MAX roll-up. The Opportunity has no matching children. What does the roll-up contain, and how do you keep the formula from returning a misleading result?
Show answer
A MAX roll-up with no matching child records is blank. Use BLANKVALUE(MaxDiscountc, 0) + 10 if a default of 0 is acceptable, or IF(ISBLANK(MaxDiscount_c), NULL, MaxDiscount__c + 10) to keep the result blank.