While working as a Salesforce system administrator, I managed orders within our Salesforce organization. Each order contains multiple product names stored in a list.
To send an email to the customer, I need to convert the list of product names into a single string with commas separating each item.
In this Salesforce tutorial, I will explain convert a list to a string in Apex using all possible methods.
Convert List to String Using String.join() Method in Salesforce Apex
In Salesforce Apex, the easiest way to convert a list to a string is by using the String.join() method. This method joins a list of strings using a specified separator (in this case, a comma).
Now, navigate to the Salesforce developer console and follow the steps below.
- In the developer console, launch the anonymous window, enter the code below, and click the Execute button.
List<String> strList = new List<String>{'Generator 200wt', 'Generator 600wt', 'Generator 1000wt'};
String result = String.join(strList, ',');
System.debug(result);- In the execution log window, select the checkbox to debug only. Then, in the debug output, the strings will be converted from a list.

This way, we can convert the list to a string in Salesforce Apex using the string.join() method.
Convert List to String using For Loop in Salesforce Apex
To convert the list to a string in Salesforce Apex using a for loop, we need to manually iterate over the list and append each element to a string variable.
Now, navigate to the developer console and follow the steps below.
- Launch the anonymous Apex window, enter the code below, and click Execute.
List<String> strList = new List<String>{'Generator 200wt', 'Generator 600wt', 'Generator 1000wt'};
String result = '';
for(String str : strList) {
result += str + ','; // Adding a separator
}
result = result.removeEnd(',');
System.debug(result); - In the execution log window, select the checkbox to debug only. In debugging, the output will return as a single string value from a list.

This way, we can convert a list to a string using the for loop in Salesforce Apex.
Convert List to String using JSON.serialize() method in Salesforce Apex
To convert a List to a String using JSON.Serialize () method in Salesforce Apex, open the developer console, and follow the steps below.
- In the Salesforce developer console, open the Execute Anonymous window, enter the code below, and click the Execute button.
List<String> strList = new List<String>{'Electric Motor', 'Electric Transformer', 'Solar inverter'};
String result = JSON.serialize(strList);
System.debug(result); - In the Execution log window, the output will return as a string in debug.

In the debug output, it appears as [“str1”, “str2”, “str3”], but the actual output is “[“str1”, “str2”, “str3″]”, which makes the whole list a string.
This way, we can convert the list to a string in Salesforce Apex using JSON.parse() method.
Convert List to String using String.valueOf() method in Salesforce Apex
In Salesforce Apex, converting a list to a string using the String.valueOf() method automatically includes square brackets and commas.
- Open the Salesforce Developer Console and launch the Apex Anonymous Window. Then, enter the code below in the anonymous window and click Execute.
List<String> strList = new List<String>{'Electric Furnace', 'Electric Arc Welder', 'Turbine Generator'};
String result = String.valueOf(strList);
System.debug(result);- In the execution log window, when we select the checkbox to debug only, we can see the list collection values converted to a string in the output.

This way, we can convert the list to a string in Salesforce Apex using the ValueOf() method.
To convert a list to a string in Salesforce Apex, use any of the above methods according to your requirements.
Conclusion
In this Salesforce tutorial, we have learned all the possible ways to convert a list to a string in Salesforce Apex. In the above steps, we have discussed the list-to-string conversion using methods such as String.join(), for loop, JSON.serialize, and String.valueOf() to convert the list to a string format in Salesforce Apex.
You may also like to read.
- Convert String to Datetime in Salesforce Apex
- Convert ID to String in Salesforce Apex
- Create Set Collection in Salesforce Apex?
- Convert String to Boolean in Salesforce Apex
- Convert String to ID 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.