1

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);
      for(p=15,p<n,p+17){
    //How to iterate through Initial_Products_Selected__c?
        thisId = LEFT(p);
        Id product = Id.valueOf(thisId);
        ProductsToInsert.add(product);
        }
    }
    insert ProductsToInsert;
}

Any help is very appreciated.

1

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(';');
2
  • 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? – Tyler Whitesides Feb 17 '15 at 4:22
  • yes, u can assign strings directly to id – Vamsi Krishna Gosu Feb 17 '15 at 4:24
1

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) {
try{
for(PS_Contract__c c : Trigger.new){
    List<String> strProductIds = c.Initial_Services_Selected__c.split('; ', 1000);
    System.debug(strProductIds);
    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;
        tasks.add(s);
        }
     insert tasks;
    }
}
catch(DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
    }
1
  • split method has an optional limit parameter. its not a mandatory one. you can simply use c.Initial_Services_Selected__c.split(';') – Vamsi Krishna Gosu Feb 17 '15 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.