You need to sign in to do that
Don't have an account?
Vladimir Bessonov
Is it possible to insert parent and child (Master Detail relationship) in one DML?
Hi. Can someone share the code sample on how to insert parent and child (Master-Detail relationship) in one DML? is it possible? I saw an example with External ID. but I assume it is not my case and I need to create an extra field that I don't need actually.
public class test {
public static void test(integer n){
// code to cretae parent record list
List<Account> parentList = new List<Account>();
List<Contact> childList = new List<Contact>();
Account Acc=new Account();
Acc.Name ='f1';
parentList.add(Acc);
insert parentList;// inserting the parent record
for(Integer i = 0; i < n; i++){
Contact con=new Contact();
con.FirstName='f1';
con.LastName='lastName1';
con.AccountId=Acc.Id;//updating the parent record after insertion
childList.add(con);
}
// insert parentList;
insert childList;
}
}
if it helps Please Mark it as Best Answer