Salesforce: Check Null/Empty Value of A Field
In Salesforce, it is very common that we build formula field, validation rule or workflow rules to act on the data of a field and sometimes we would like to validate on an empty value. In Salesforce formula editor, there are two functions: ISBLANK() and ISNULL(). The question here is, which is the correct one that we should use?
This is an old function that works with most of the field types except Text, Text Area and Long Text. (reference)
This is a new function that Salesforce introduce to support the empty field validation on Text fields. It also works with other field types. According to Salesforce documentation, it is recommended to use this function instead. (reference)
Please note that if you are referencing a formula field with the option “Treat blank fields as zeroes”, the formula field will give value zero and won’t be considered as null.
Above is how you can validate an empty field in formula editor. How can we achieve the same in Apex?
There are a few options to validate empty text fields in Apex:
Use String.isBlank() method. This will return true if the text field is empty.
Compare the text field to an empty string, e.g, Account.Name == ”. You shouldn’t use NULL as text field in Salesforce is never considered NULL. Note that String variable can still be considered NULL.
This can be compared by using TRUE or FALSE. This is because an unchecked checkbox field is considered as FALSE instead of NULL.
You should compare the field value to a NULL.