Check if a String is Null, Empty or Blank in Apex

Check if a String is Null, Empty or Blank in Apex

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?

Objectives:

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.

  1. Null: It means that a variable or field does not have a value assigned to it. For example:
  2. 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:
  3. Blank: It means that a variable or field is null, empty, or contains only whitespace characters. For example:

Let’s take a pause here, familiarize yourself with the isBlank() & isEmpty() string methods. 

Method Name
Details
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):

  1. 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.
  2. 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.
  3. 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:

  1. Click on Setup | Developer Console. 
  2. I will be using Execute Anonymous Apex tool in the Developer Console to run the Apex code.
    1. 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.
  3. 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));

  4. 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.
  5. 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:

Formative Assessment:

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.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Preferred Timing(required)

2 thoughts on “Check if a String is Null, Empty or Blank in Apex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Automation Champion

Subscribe now to keep reading and get access to the full archive.

Continue reading