While working on a Salesforce development project, I was debugging an Apex class that was supposed to update an account record based on its ID, but I kept encountering an “Invalid ID” error.
Then, I tested and validated a string conversion to an ID type in the developer console to ensure the conversion logic worked correctly. There, I used the function Id.valueOf(strId) and was able to convert the string to Id.
In this Salesforce tutorial, I will explain how to convert a string data type to an ID value in Salesforce Apex.
Convert String to ID in Salesforce Apex
To convert the string to ID in Salesforce Apex, navigate to the developer console and follow the steps below.
- Create an Apex class in the developer console, and for that, click on File > New > Apex Class.
- Enter the name for the apex class and click OK.

- Enter the code below in the developer and save the code.
public class ConvertStringToId {
public static Id convertStringToId(String strId) {
Id convertedId;
convertedId = Id.valueOf(strId);
return convertedId;
}
}- ConvertStringToId.Id: This shows that the method will return an ID-type value from the string.
- (String strId): The method takes one input parameter named str Id of type String.
- Id.valueOf(strId): This is a built-in method provided by Salesforce that converts a String to an Id type. If the provided string is not a valid Salesforce ID format (e.g., not 15 or 18 characters long), it will throw a System.StringException.
- Return convertedId: It will return the converted Id.
- After entering and saving the code, if there are no errors, then press ” Ctrl + E” to execute the code.
- In the anonymous window, enter the code below, then select the checkbox, open in Log, and click the Execute button.
String accountIdString = '0015i00000lU1htAAC';
Id resultId = ConvertStringToId.convertStringToId(accountIdString);
System.debug('Resulting Id: ' + resultId);Here, “Id resultId” will call the method from the class to convert the string to Id.

- In the debug console, select the checkbox to debug only. Then, you will see that the entered string ID is returned as ID.
This way, we can convert a string to an ID in Salesforce Apex by using the function ID.valueOf (strID) in the Apex class.
Convert a List String to Set of IDs in Salesforce Apex
In the above method, we converted a single string to an ID, but we can also convert a batch of record data from a string to an ID in Salesforce Apex.
In the steps below, I will explain how to convert a list of strings to a set of IDs in Salesforce Apex.
Now, navigate to the Salesforce developer console and follow the steps below.
- Create a new Apex class, and for that, select File > New > Apex Class.
- Enter the name of the new apex class and click OK.
- Enter the code below in the developer console and save it.
public class ConvertStringToListSet {
List<String> stringList;
Set<Id> idSet;
public ConvertStringToListSet() {
stringList = new List<String>{
'0015i000018U7qyAAC',
'0015i000013T7GNAA0',
'0015i00000v5rIRAAY'
};
idSet = new Set<Id>();
for (String str : stringList) {
if (String.isNotBlank(str) && str.length() == 18) {
idSet.add(Id.valueOf(str));
}
}
System.debug('Set of IDs: ' + idSet);
}
}Now, let’s understand the above code, through which we converted the list of strings to a set of IDs.
- In the class stringList, we have taken a list of Account record IDs.
- Set idSet: A set to hold unique Id objects converted from strings in stringList.
- The constructor initializes stringList with predefined 18-character Salesforce ID strings and idSet as an empty set to store IDs.
- Now, the for loop iterates through each list of strings. We have used the function below in the loop.
- String.isNotBlank(str): Ensures the string is not null or empty.
- Str.length() == 18: Ensures the string is exactly 18 characters long, the standard length for a Salesforce ID.
- If both checks pass, the string is converted to an Id type using Id.valueOf(str) and added to idSet.
- At last, System.debug() displays all the valid Id objects converted from stringList.
- If no errors are in the Problems tab, execute the code with the “Ctrl+E” shortcut keys.
- Enter the code below in the anonymous window, then select the “open the log” checkbox and click the Execute button.
ConvertStringToListSet instance = new ConvertStringToListSet();
- In the console log window, select Debug only and click the Execute button.
In the debug window, the list of strings is converted and displayed as a set of IDs.

This way, we can convert a list of strings to a set of IDs in Salesforce Apex by following the above steps.
Conclusion
In this Salesforce tutorial, we have learned to convert a string to an ID in Salesforce Apex. I have explained the methods for converting a single string to an ID using the function Id.valueOf(strId) and how you can convert a list of strings to a set of IDs in Salesforce Apex using the same function.
You may also like to read:
- Convert String to Date in Salesforce Apex
- Convert a String to an Integer using Apex in Salesforce
- Reset and Change User Password Using Salesforce Apex
- Convert Integer Data Type 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.