+ Start a Discussion
salesforce User 15salesforce User 15 

Create Child records when parent record is created

When a parent record is created how to automatically create 5 child records of different types? Can any one help me how to develope this. Should I have to use apex trigger? any thing else to create these child records?
Sonam_SFDCSonam_SFDC
Yes, you can use a trigger for creation of child records.

The following sample code to create contacts(child) when account(parent) is created will help you create your own trigger:

trigger CreateAccountContact on Account (after insert, after update){

if(Trigger.isInsert){

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){

        Contact c = new Contact(LastName = acc.name,
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    /* similarly add all fields which you want */
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Phone=acc.Phone);

        ct.add(c);
    }
    insert ct;
}