I am building a Flow that takes a user through a wizard to create a custom contract object with detail objects that represent the products on that contract (stored as a junction object). An issue I ran into is that Flow has a known limitation where if you use a Dynamic Choice (i.e. creating a dynamic set of checkboxes to choose Products from based on records from the Product object) it can only store the choices as a long semicolon delimited string in a variable; it cannot create detail objects based on your dynamic selection (even using a loop).

To get around this, I want the flow to just create the parent Contract, then I want an after insert Apex trigger to parse the field where the long string of semicolon separated Product Ids are stored. The trigger would need to take the string and find each Id to store in a List. Once I had the List made from the text field I could insert the Product detail records to the parent Contract.

I am very new to Apex, mostly just a point and click admin here. I am struggling with how to use RIGHT and LEFT to loop through the string field.

trigger AddProductstoSOW on PS_Contract__c (after insert) {
        for(PS_Contract__c c:Trigger.new){
        List<Id> ProductsToInsert = new List<Id>;
        integer n = LEN(c.Initial_Services_Products__c);
    //How to iterate through Initial_Products_Selected__c?
        thisId = LEFT(p);
        Id product = Id.valueOf(thisId);
    insert ProductsToInsert;

Any help is very appreciated.

2 Answers 2


You can use the String split method and split the concatenated string using the delimiter. something like this

 List<String> strProductIds = c.Initial_Services_Products__c.split(';');
  • Thanks. I will work with that and see if I can get anywhere. If that is storing a list of strings, how would you suggest converting that into Ids so that they could be used to create detail records? Or would the string value be interpreted as an Id? Commented Feb 17, 2015 at 4:22
  • yes, u can assign strings directly to id Commented Feb 17, 2015 at 4:24

Thank you for the assistance. I was able to resolve the issue. Though with .split() you also seem to need an upper limit parameter. Took a little struggling to figure this out but here it is:

trigger AddProductstoSOW on PS_Contract__c (after insert) {
for(PS_Contract__c c : Trigger.new){
    List<String> strProductIds = c.Initial_Services_Selected__c.split('; ', 1000);
    List<SOW_Task__c> tasks = new List<SOW_Task__c>();
        for(String i : strProductIds){
        SOW_Task__c s = new SOW_Task__c();
        s.PS_ServiceNo__c = i;
        s.PS_Contract__c = c.Id;
     insert tasks;
catch(DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
  • split method has an optional limit parameter. its not a mandatory one. you can simply use c.Initial_Services_Selected__c.split(';') Commented Feb 17, 2015 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .