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
1 Answer
Here is an example with Account and Contact.
Let's say Account has MultiSelectPicklist__c
which holds the values like this:
A1;A2;A3;A4
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);
lstCon.add(con);
}
insert lstCon;
}
-
i am trying to execute above code,but not getting the value in str/– shwetaCommented Jul 3, 2017 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;}}– shwetaCommented Jul 3, 2017 at 11:48
-
did you investigate that, which line is giving error? what is get printed in debug log? Commented Jul 3, 2017 at 15:37
-
String str = acct.MultiSelectPicklist__c; this give str =0 in debug,but opportunity have values in multi picklist field.– shwetaCommented Jul 4, 2017 at 6:30
-
I have given an example how to parse, you need to use this kind of code based on your usecase Commented Jul 4, 2017 at 13:30