how to create child records on the basis of parent record field multiselect picklist,if picklist have 1 value,create single child record.if picklist have 2 values,create 2 records of child


Here is an example with Account and Contact.

Let's say Account has MultiSelectPicklist__c which holds the values like this:


Then it will first create a List splitting the by ';' and then looping through the lstValues create a list on Contacts and then insert the Contact List.

Account acct = [Select Id, MultiSelectPicklist__c FROM Account WHERE Id = '<record Id>'];

String str = acct.MultiSelectPicklist__c;
List<Contact> lstCon = new List<Contact>();

if(str !=null)
    List<String> lstValues = str.split(';');
    for(String pkValue: lstValues)
        Contact con = new Contact(AccountId = acct.Id, LastName='LastName ' + pkValue);
    insert lstCon;
  • i am trying to execute above code,but not getting the value in str/ – shweta Jul 3 '17 at 11:44
  • List<Opportunity_Splits__c> lstCon = new List<Opportunity_Splits__c>(); Opportunity oppt= [Select Id, Practice__c FROM Opportunity WHERE id IN :OpportunityIds]; system.debug('-----oppt--------'+oppt); String str = oppt.Practice__c ; system.debug('-----str --------'+str ); if(str !=null) { List<String> lstValues = str.split(';'); for(String pkValue: lstValues) { Opportunity_Splits__c con = new Opportunity_Splits__c(Opportunity__c = oppt.Id, Practice__c=pkValue); lstCon.add(con); } insert lstCon;}} – shweta Jul 3 '17 at 11:48
  • did you investigate that, which line is giving error? what is get printed in debug log? – Santanu Boral Jul 3 '17 at 15:37
  • String str = acct.MultiSelectPicklist__c; this give str =0 in debug,but opportunity have values in multi picklist field. – shweta Jul 4 '17 at 6:30
  • I have given an example how to parse, you need to use this kind of code based on your usecase – Santanu Boral Jul 4 '17 at 13:30

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.