How to Convert List to String in Salesforce Apex

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.

  1. 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);
  1. In the execution log window, select the checkbox to debug only. Then, in the debug output, the strings will be converted from a list.
Convert list to string in Salesforce apex

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.

  1. 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); 
  1. 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.
Salesforce apex convert list to string

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.

  1. 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); 
  1. In the Execution log window, the output will return as a string in debug.
Salesforce apex list to string conversion

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.

  1. 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);
  1. 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.
Convert list to string in Salesforce apex using ValueOf method

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.

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.