Hello everyone, this post will show you how to convert multi-line String to List in Flow Builder using Apex Invocable Method.

In order to achieve this, we can create an Apex class and the test class as well.

// Apex Invocable class - StringToListUtil.apxc
public class StringToListUtil {
    
    @InvocableMethod(label='Convert String to List')
    public static List<List<String>> convertStringToList(List<String> inputParamStrings) { 
        
        List<List<String>> resultList = new List<List<String>>();
        
        for(String inputParamString : inputParamStrings){
            if(String.isBlank(inputParamString)) continue;
            
            List<String> innerResultList = new List<String>();
            for(String str : inputParamString.split('\r\n')){
                if(String.isBlank(str)) continue;
                innerResultList.add(str.trim());
            }
            resultList.add(new List<String>(innerResultList));
        }
 
        return resultList;
    }
}
// Apex test class - StringToListUtilTest.apxc
 
@isTest
public class StringToListUtilTest {
 
    @isTest
    public static void testConvertStringToListUtilMethod() {
        String str = '1\r\n2\r\n3';
        List<List<String>> res = StringToListUtil.convertStringToList(new List<String>{str});
        System.assertEquals(res.get(0), new list<String>{'1','2','3'});
    }
}

Next, let’s create a Screen Flow in Flow Builder. In order to demonstrate it in the simplest way, I have created this Flow with three components:

image

  1. First Screen - Take the string input
  2. Apex Action - Pass in the string input and assign the output to a collection variable
  3. Next Screen - Output and see the result

In the first screen, we add a Long Text Area Component to the screen:

image

Next, we can add the Apex Action. We can pass the field into the input values and store the output values to your collection variable.

image

Once the string is converted to a list, we can output these variables to confirm the result:

image


That’s it! Hope you learn something useful today! See ya!