Recently, the sales team created a large number of leads manually in our Salesforce org. Information such as phone numbers and emails is crucial for Salesforce leads.
To prevent lead creation with missing or invalid email addresses (e.g., spaces or blank values), we implemented an Apex trigger that verifies the Email field is not null, empty, or contains only whitespace before inserting the lead record.
In this Salesforce tutorial, I will explain how to check if a string is Null, Empty, or Blank in Salesforce Apex.
Methods to Check if a String is Null, Empty, or Blank in Salesforce Apex
In Salesforce Apex, we can use the following methods to check whether a string is Null, Empty, or Blank.
- IsBlank – The syntax for using this function with a string is IsBlank(InputString), which returns true if the specified String is white space, empty (”), or null; otherwise, it returns false.
- IsEmpty – It returns true if the specified String is empty (”) or null, otherwise it returns false.
Use case of the IsBlank and IsEmpty methods in Salesforce Apex strings or text fields.
- To check if a string is null, you can use the isBlank() method. It returns true if the string is null or contains only whitespace.
- To check if a string is empty (contains no characters), you can use the isEmpty() method. It returns true if the string has zero length.
- You can combine the checks using the isBlank () method to check if a string is blank (null, empty, or contains only whitespace).
Check if a String is Null, Empty, or Blank in Salesforce Apex
Follow the steps below to check if a String in the Salesforce Apex code is null, empty, or blank.
- Navigate to the Salesforce developer console and launch the anonymous window with the shortcut keys “Ctrl+E.”
- In the anonymous window, enter the code below and click the Execute button.
public with sharing class EmailStringValidator
{
public String email {get; set;}
public EmailStringValidator()
{
email = 'stauart.b11@mail.com'; // Default email for testing
}
// Check if the email is blank (null, empty, or whitespace)
public Boolean isBlankEmail()
{
if (String.isBlank(email))
{
return true;
}
else
{
return false;
}
}
// Check if the email is empty, not null
public Boolean isEmptyEmail()
{
if (String.isEmpty(email))
{
return true;
}
else
{
return false;
}
}
} - In the above apex code, we have defined methods using the functions isBlank and isEmpty to validate the email field and check if it is null, empty, or blank. The methods will return the validation in Boolean form as True or False.
- Now launch the anonymous window with the keys “Ctrl+E.” We will test the above apex code there.
- Enter the code below in the anonymous window and click the Execute button.
EmailStringValidator emailChecker = new EmailStringValidator();
// to check null, empty, or space
Boolean isBlank = emailChecker.isBlankEmail();
System.debug('Is Email Blank: ' + isBlank);
// to check empty string, not null
Boolean isEmpty = emailChecker.isEmptyEmail();
System.debug('Is Email Empty: ' + isEmpty);- In the Console log window, we will see the output of the debug code. When we entered the email field (string), the output was as follows:

- When the email field (string) was blank, the output was as follows:

- When the email field (string) was whitespace, the output was as follows:

This way, we can check if a String is Null, Empty, or Blank in Salesforce Apex by following the above methods.
Conclusion
In this Salesforce Apex tutorial, we have learned how to check if a string is Null, Empty, or Blank in Apex using the functions isBlank and isEmpty. With the help of these methods, you can easily validate the strings in your Apex code.
We also learned about the isBlank and isEmpty functions in Apex, and later, we used those methods in the Apex class to check whether a string value is Null, Empty, or Blank.
You may also like to read.
- Split String Method in Salesforce Apex
- Use Switch Statement in Salesforce Apex
- Reset and Change User Password Using Salesforce Apex
- Truncate String in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.