Convert a String to an Integer using Apex in Salesforce

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.

  1. In the developer console, click on the File > New > Apex Class. Enter the name of the Apex class and click OK.
Convert string to integer using apex in Salesforce
  1. 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' );
            
        }
    }
}
  1. 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 ‘intValvariable.
  • 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.
How to convert string to integer in Salesforce
  1. 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();
Salesforce apex convert string to integer
  1. 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.
String converted to integer in Salesforce Apex

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.

  1. 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;
    }
}
  1. 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));  
Check Probability of string to integer in Apex Salesforce
  1. 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.
How to check String will convert to integer in Salesforce apex

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:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.