Last Updated on June 14, 2023 by Rakesh Gupta
Big Idea or Enduring Question:
- How to check in Apex if a Text field is null, empty or blank?
After reading this blog, you’ll be able to:
- Differentiate between null, empty and blank
- Check if a text field field is null
- Check if a text field field is empty
- Check if a text field field is blank
- and much more
Business Use case
Ben Halpern is a Jr. Salesforce Developer at Gurukul On Cloud (GoC). Ben wants to learn how to check if a text field is null, empty or blank in Apex.
Automation Champion Approach (I-do):
It’s very easy to confuse a null, empty or a blank string for beginners. However, let’s go by the definition of each to draw a line of difference.
- Null: It means that a variable or field does not have a value assigned to it. For example:
String myStr1 = null;
- Empty: It means that a variable or field has been assigned a value, but that value is an empty string with zero characters. For example:
String myStr2 = '';
- Blank: It means that a variable or field is null, empty, or contains only whitespace characters. For example:
String myStr3 = ' ';
|isBlank()||Returns true if the specified String is white space, empty (”), or null; otherwise, returns false.|
|isEmpty()||Returns true if the specified String is empty (”) or null; otherwise, returns false.|
Guided Practice (We-do):
- To check if a Text field is null, you can use the isBlank() method. It returns true if the string is null or contains only whitespace.
- To check if a Text field is empty (contains no characters), you can use the isEmpty() method. It returns true if the string has zero length.
- To check if a Text field is blank (null, empty, or contains only whitespace), you can combine the checks using the isBlank() method.
Using these methods, you can effectively determine whether a Text field is null, empty, or blank in Apex. Let’s see this in action:
- Click on Setup | Developer Console.
- I will be using Execute Anonymous Apex tool in the Developer Console to run the Apex code.
- An anonymous block is an Apex code that does not get stored in the metadata but, can be compiled and executed. Anonymous blocks execute as the current user and can fail to compile if the code violates the user’s object – and field-level – permissions.
- To open Execute Anonymous window, click on Debug | Open Execute Anonymous Window.
String myStr1 = null; String myStr2 = ''; String myStr3 = ' '; System.debug('Null Check - ' +String.isBlank(myStr1)); System.debug('Empty Check - ' + String.isEmpty(myStr2)); System.debug('Blank Check - ' +String.isBlank(myStr3));
- To execute all codes in the window, click on the Execute button. Make sure to select Open Log check box. Once you do that, the log will automatically open in the Log Inspector after the code executes.
- The debug log will be listed on the Logs tab. Double-click the log to open it in the Log Inspector. In our case, the log will automatically open after the code executes. To better analyze the variables’ run time value, make sure to select the Debug Only check box, as shown in the following screenshot:
I want to hear from you!
What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.