Recently, I fixed a bug in a custom Salesforce Application where users had the option to turn off/on notifications. The preferences are stored as strings in a custom field on a User_Preference__c object. The possible values for this field are “true” or “false.”
I had to convert these string values to Boolean values for the Apex code for conditional logic. To solve this, I used an Apex method that reads the user preference string, converts it to a Boolean, and then uses it to determine whether to send a notification.
In this Salesforce tutorial, I will explain how to convert a string to a boolean in a Salesforce Apex Class.
Convert String to Boolean in Salesforce Apex
To convert a String data type to a Boolean, navigate to your Salesforce developer console and follow the steps below.
- In the developer console, create a new class by selecting File > New > Apex Class.
- Enter the new class’s name and click the OK button.
- Enter the code below in the developer console and save it.
public class ConvertStringToBoolean {
public static Boolean convertStringToBoolean(String str) {
Boolean result;
if (String.isEmpty(str)) {
result = false;
} else {
result = Boolean.valueOf(str);
}
return result;
}
}- After entering the code, if there are no errors in the apex code, execute it using the shortcut keys “Ctrl+E.”
- We will test the apex code for boolean and non-boolean values in the anonymous window, and for that, enter the code below.
String strTrue = 'true';
String strFalse = 'false';
String strInvalid = 'notaboolean';
Boolean boolTrue = ConvertStringToBoolean.convertStringToBoolean(strTrue);
Boolean boolFalse = ConvertStringToBoolean.convertStringToBoolean(strFalse);
Boolean boolInvalid = ConvertStringToBoolean.convertStringToBoolean(strInvalid);
System.debug('True String Converted to Boolean: ' + boolTrue);
System.debug('False String Converted to Boolean: ' + boolFalse);
System.debug('Invalid String Converted to Boolean: ' + boolInvalid); - In the Console log window, select the checkbox Debug only, then you will see the output of the three values we have entered.
Here, in the output, we got True for the True string, False for the False string, and for the invalid string, we also got False as output.

By following the above steps, you can convert a string data type to a Boolean in Salesforce Apex.
Illegal conversion from String to Boolean in Salesforce Apex
In Salesforce Apex, you get an “Illegal Assignment” error while converting a string value to a boolean. This error occurs while compiling the code because Apex is a strongly typed language that checks a variable’s data type during compile time.
In Salesforce, the apex conversion of a data type should be valid; otherwise, the Illegal conversion error will occur.
For example, as shown in the code below, if you tried to assign a boolean directly, the code would result in a compilation error.
String myString = 'true';
Boolean myBool = myString;Apex does not automatically convert strings like ‘true’ or ‘false’ to their Boolean equivalents. Therefore, we need to handle such conversions with the built-in functions.
In the steps below, we will see the correct method of string-to-boolean conversion in Salesforce.
- Navigate to the Salesforce developer console and open the anonymous window with
The code below is the correct way to convert a string to a boolean in Salesforce Apex.
String myString = 'true';
Boolean IsBool = Boolean.valueOf(myString);
System.debug('Boolean value: ' + IsBool); - After entering the code in the anonymous window, click Execute to run the code.
- In the console log window, select the checkbox to debug only, and there, you will get the converted string to a boolean output.

This is the correct way to convert a String to a Boolean data type in Salesforce Apex.
Handle Invalid Values String to Boolean Conversion in Salesforce Apex
While converting a string to a Boolean in Salesforce Apex, we can handle exceptions where the string might not be a valid Boolean value. It would help if you validated the string before conversion to avoid unexpected errors.
- Navigate to the Salesforce developer console and open the anonymous window with the shortcut keys “Ctrl+E.”
- Enter the code below in the anonymous window, select the checkbox, open it in debug, and click the Execute button.
String myString = 'notTrue';
Boolean myBool;
try {
myBool = Boolean.valueOf(myString);
if (myBool == null){
System.debug('The string is not a valid Boolean value.');
} else {
System.debug('Boolean value: ' + myBool);
}
} catch (Exception e) {
System.debug('Error converting String to Boolean: ' + e.getMessage());
}In the above code, I have entered the string value as “not true,” which is an invalid value for the string-to-boolean conversion.
Then, we used the try-and-catch method to handle the invalid string value so that it would not impact the rest of the code and would run.
- In the console log window, select the checkbox Debug only, and then you will see the output as False. Here, we didn’t get any errors like “Illegal assignment.”

This way, we follow the above steps to handle the Invalid values string-to-boolean conversion in Salesforce Apex.
Conclusion
In this Salesforce tutorial, we learned how to convert a string to a Boolean in Salesforce Apex using the Boolean.valueOf(str); function, and with this, we converted a string value to a boolean. We also learned to handle errors like “Illegal assignment” while converting a string value to a boolean in Salesforce Apex.
You may also like to read:
- Convert String to ID in Salesforce Apex
- Convert String to Date in Salesforce Apex
- Convert a String to an Integer using Apex in Salesforce
- Convert a String to Decimal 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.