function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jay reddyJay reddy 

Apex to create multiple child records when a parent record is created

Heya mates,

I'm trying to create multiple child records with the help of apex, when the parent record is created. My issue is there is picklist field on Child object with 20 values and when we create a parent record, the apex should create 20 different child records based on the picklist value on child object.

OBJECTS: CustomObjA, CustomObjB [Masterdetail]

I know below code is very ametuer, which explains my range in apex coding. Also, the code should be builkfied.
 
trigger CreateBuyingInfluence on CustomObjA (after insert)

 {
    List<CustomObjB> Childs = new List<CustomObjB>();

    for(CustomObjA a : trigger.new)
    {
       CustomObjB Child = new CustomObjB ();
       Child.SCOP__c = a.id;
       Child.Name = 'testName'; 

       Childs.add(Child);      
    }

    insert Childs;
}

 
Raj VakatiRaj Vakati
trigger CreateBuyingInfluence on CustomObjA (after insert)

 {
    List<CustomObjB> Childs = new List<CustomObjB>();
  Schema.DescribeFieldResult fieldResult =
 CustomObjB__c.SCOP__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
   
   
    for(CustomObjA a : trigger.new)
    {
		for(Schema.PicklistEntry p :ple){
       CustomObjB Child = new CustomObjB ();
       Child.SCOP__c = a.id;
       Child.Name = p.getValue(); 
		}

       Childs.add(Child);      
    }

    insert Childs;
}

You need to write some tthink like abobe
Raj VakatiRaj Vakati
Refer this link 

https://developer.salesforce.com/blogs/developer-relations/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html
Jay reddyJay reddy
Hi @Raj V

Really thanks for your help. But, actually, can we do this with just APEX CLASS instead of trigger, because I want to invoke the class from a process builder along with other actions.
Santosh Bompally 8Santosh Bompally 8
Hi Jay, 
Use Raj logic as is, Just add an invocable method around it, 
 
public class CreateBuyingInfluence {   
  @InvocableMethod     
public static void BuyingInfluence(List<CustomObjA > MultipleAs)    
 {            
// Raj Logic as is, just replace trigger.new with MultipleAs 
} }

 
Raj VakatiRaj Vakati
Here is the code
 
public class CreateBuyingInfluence {   
  @InvocableMethod(label='Insert Accounts' description='Inserts the accounts specified and returns the IDs of the new accounts.')
public static void insertCustomObjA(List<CustomObjA > MultipleAs)    
 {            

 List<CustomObjB> Childs = new List<CustomObjB>();
  Schema.DescribeFieldResult fieldResult =
 CustomObjB__c.SCOP__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
   
   
    for(CustomObjA a : MultipleAs)
    {
		for(Schema.PicklistEntry p :ple){
       CustomObjB Child = new CustomObjB ();
       Child.SCOP__c = a.id;
       Child.Name = p.getValue(); 
		}

       Childs.add(Child);      
    }

    insert Childs;
 
 } 
 
}