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
Sai KulkarniSai Kulkarni 

How to create Parent and Child Records in One Insert Call? in apex controller

Best Answer chosen by Sai Kulkarni
Shruti khodaboleShruti khodabole
Hi,
The simplest way to achieve this is to insert the parent, then set the lookup relationship id on the child to the parent record id, as follows:

Account acc=new Account(Name='Blog Acc1');
insert acc;
 
Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', AccountId=acc.id);
insert cont;

Also you can refer:http://bobbuzzard.blogspot.in/2012/03/create-parent-and-child-records-in-one.html

Thanks,
Shruti Khodabole
Salesforce Developer
http://www.zen4orce.com

All Answers

Shruti khodaboleShruti khodabole
Hi,
The simplest way to achieve this is to insert the parent, then set the lookup relationship id on the child to the parent record id, as follows:

Account acc=new Account(Name='Blog Acc1');
insert acc;
 
Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', AccountId=acc.id);
insert cont;

Also you can refer:http://bobbuzzard.blogspot.in/2012/03/create-parent-and-child-records-in-one.html

Thanks,
Shruti Khodabole
Salesforce Developer
http://www.zen4orce.com
This was selected as the best answer