Recently, while building a custom class for user input in my Salesforce org, where I was customizing the fields for the Contact object, I noticed a custom field ‘age’ in the contact that stores the data in string format. I had to convert this string data into an integer to make it convenient to validate and process the object data.
In this Salesforce tutorial, I will show you how to convert string data to an integer using Apex in Salesforce.
Convert a String to an Integer using Apex in Salesforce
To convert a string data type to an Integer one using Apex in Salesforce, navigate to the Salesforce developer console and follow the steps below.
- In the developer console, click on the File > New > Apex Class. Enter the name of the Apex class and click OK.

- Enter the code below in the new apex class’s parentheses ‘ { } ‘.
public class StringToInteger {
public static void ConvertStringToInteger() {
String str = '65';
if ( str.isNumeric() ) {
Integer intVal = Integer.ValueOf( str );
System.debug( 'display result when string convert to integer ' + intVal );
} else {
System.debug( 'String ' + str + ' is not numeric' );
}
}
}- Now, we will break down the structure of the above code.
- In the public class, we used the public static method ConvertStringToInteger. Since it is static, it can be called without creating an instance of the StringToInteger class. The method does not return any value (void).
- Here, ( str.isNumeric() ) checks if the string str consists only of numeric characters. Then ‘.isNumeric()‘ is a method available on the String Class that returns true if the string is numeric; otherwise, it is false.
- The function ‘ Integer.valueOf(str) ‘ converts the numeric string ‘str‘ to an integer and assigns it to the ‘intVal‘ variable.
- To execute the code, click on Debug in the menu bar and select Open to the anonymous window. We can also execute the code using the ‘Ctrl + E’ shortcut keys.

- An anonymous window will appear when you click the Execute button. Here, we will enter the execution code to run it in the console.
- Enter the code below in the execution code window, then select the checkbox ‘Open Log‘ and click Execute.
- Simply enter the Class name followed by a dot (.), select the auto-suggested code, and then execute it.
StringToInteger.convertStringToInteger();
- When we click on the execute button, the code runs in the debug log. Here, we only have to view the output, so we select the Debug checkbox.
- Now, in the execution log, we can see the debug details of the code. In the details, we can see that the conditions were true and that the code was executed successfully.

This way, we can convert the string data type to an integer using Apex in Salesforce with the help of Integer.ValueOf( str ) function.
Check the Possibility of converting a string to an Integer type in Salesforce Apex
There is a method for knowing if an exception will be thrown when converting a string to an Integer. I will explain how to determine whether the string is convertible to an integer using the loops in Salesforce Apex.
Now, navigate to your developer console and follow the steps below.
- In the nav bar of the developer console, click File > New > Apex Class.
- Enter the name for the new apex class and click OK.
- Enter the code below in the developer console.
public class CheckConvertProbability {
public static Boolean isConvertPossible(String str) {
if (str != null) {
try {
Integer.valueOf(str);
return true;
} catch (TypeException e) {
return false;
}
}
return false;
}
}- Save the code and press ‘ Ctrl+e ‘ to execute the code.’
- In the Anonymous window, enter the code below. After this, select the checkbox Open Log and click the Execute button.
System.debug(CheckConvertProbability.isConvertPossible('123'));
System.debug(CheckConvertProbability.isConvertPossible('abc'));
System.debug(CheckConvertProbability.isConvertPossible(null)); 
- In the console log window, select the checkbox to debug only. You will notice that only the first debug returned True. In contrast, when we used the strings ‘three‘ and ‘Null‘ in the second and third debugs, the values were False.

So, when you execute the above code and get the value as True in debug, the string value you have entered can be converted to an integer in Salesforce Apex.
This way, you can check the probability of converting a string to an Integer type in Salesforce Apex by following the above steps.
Conclusion
By now, you will be able to convert the string data type to an integer in your Salesforce Apex environment and can more efficiently manage string data conversion. In this Salesforce tutorial, we have also learned to check the possibility of converting a String value into an integer data type. By checking the probability, you can avoid errors that can occur due to incorrect string data.
You may also like to read:
- Salesforce Application to calculate Business Days using Apex
- Convert Integer Data Type to String in Salesforce Apex
- Variables and Data Types in Salesforce Apex
- Convert String to Date in Salesforce Apex

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.