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
salesforce User 15salesforce User 15 

Create Child Object records when Parent Object record is created

Hi All,

Can any one help me to find the best way to accomplish this requirement?

When a parent Object record is created I have to populate 5 child object records. By using Apex trigger I can do this or I have to develop a VF page? Please guide me on this. Thanks in advance!!!
Best Answer chosen by salesforce User 15
Madhura BMadhura B

Hi,

You will have to write a trigger. Here's an example for creating 5 records in the child object.

trigger createChild on obj_1__c (after insert) 
{
   List<obj_2__c> lstObject2 = new List<obj_2__c>();
    for (obj_1__c con : trigger.New) 
    {
       for(integer i=0; i< 5; i++ )
       {
          obj_2__c ob2 = new obj_2__c();
          ob2.obj1__c = con.id;
          lstObject2.add(ob2);
       }
    
    }
    insert lstObject2;
}

obj_1__c  is the parent and obj_2__c is the child object

 


Please mark this as the answer if this solves your issue

Thanks