How to Convert Integer Data Type to String in Salesforce Apex

While working on the Account object, I created the Account ID field using the Integer data type. However, the sales team wanted to store the account ID in alphanumeric form, which was not possible with the integer data type.

I was required to convert the Integer data type to a string in Salesforce Apex. In this tutorial, I will explain how I achieved this requirement and converted the integer data type to a string using different Apex methods.

Integer Data Type in Salesforce Apex

In Salesforce, Integer data type variables can store positive and negative numeric values. It is used to store short numbers or non-fractional values, which can be stored within the -2117483648 to +2117483647 range.

This data type has another subtype, Long. The extended data type is used to store more extended numbers, which can store values from -2^63 to +2^63-1. If you want to store big numbers, then you can use long data types.

Syntax:

integer i = 50; 
long no = 2145689464654L; // enter L as a suffix, either capital or small.

Convert Integer Data Type to String in Salesforce Apex

In Salesforce, we can convert an Integer data type to a String using several methods. I explained some methods in the following Apex program.

1. Convert Integer to String Using String.valueOf() Method

public class ConvertDataType {
        public void IntegerToString(){
              Integer no = 150;
              String strValue = String.valueOf(no);
              System.debug('The Interger has been sucessfully converted to the String: ' +strValue);
    }
}

Here, I created a variable named no with an Integer as the data type, and we wanted to convert it to a string. For that, we have String.valueOf() method. We need to pass an integer variable as a parameter in the method.

The method converts integer variables to the string data type and stores the result in the strValue variable, which is of the String data type.

How to Convert Integer Data Type to String in Salesforce Apex

The output strValue variable displays the integer variable value even though it is a string, which means the Integer data type has been successfully converted to a String.

In this way, we can convert an Integer data type to a string using the String.valueOf() method in Salesforce Apex.

2. Convert an Integer to a String Using the Format() Method

public void IntegerToString() {
     Integer no = 123;
     String str = no.format();
     System.debug(str); 
}

I created no variable as an Integer data type and str as a String. Then, using the format method, we need to add an integer variable to the format method and store the converted value in the str variable, which is a string in data type. As we display the str in the output, it displays the integer variable value.

In this way, we can convert an Integer data type to a String using the format() method in Salesforce Apex.

Convert the list of integers to a list of strings using Salesforce Apex

The list is a collection data type in Salesforce, and we can store other data types’ values in the list. Here, I will explain how to convert an Integer list to a String list using Apex in Salesforce.

Convert an Integer List into a String List Using a Loop in Apex

public class ConvertDataType {
    
    public void IntegerToString(){
        
        List<Integer> intList = new List<Integer>{1, 2, 3, 4, 5};
        List<String> strList = new List<String>();
        for (Integer i : intList) {
            strList.add(String.valueOf(i));
            }
        System.debug(' String List: ' +strList);
    }
}

Here, we have a list of integer values stored in the intList variable, and we want to convert this list into string values. For that, we need to create an empty list of strings so that after converting integer values, we can add them to an empty string list.

Then, using the for loop, we pass intList values to the user-defined variable, which is also an integer data type. This for loop iterates over the integer list and passes one value to the i variable.

Using the String.valueOf(i) method, we convert that value from an integer to a string and simultaneously use the strList.add() method to add that converted value to the empty list of strings.

In the debug method, we called the strList, which is a list of strings, even though it displays the integer list values, indicating that a list of integers has been converted to a list of strings.

How to convert Integer to String Using Apex in Salesforce

In this way, we can convert a list of Integer data types to a list of Strings using a loop in Salesforce Apex.

Conclusion

I hope you have an idea about converting Integer data types to strings in Salesforce. In this tutorial, I explained how to convert an integer to a string using several methods. Then, we converted a list of integers to a list of strings in Salesforce Apex. In the apex program that we saw in the String.valueOf() method to convert an integer to a string in Salesforce Apex.

You may also like to read:

live webinar

Data Cloud in Agentforce Data Library

In this live webinar, we will showcase how Salesforce AI Agents use business data and documents to provide intelligent responses using Agentforce Data Library and Salesforce Data Cloud.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

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