I saw an example how to insert Parent and Child Record in one DML Statement from Developer Guide and It's Working Fine no issue in that.
Here In my case I would like insert List Of Both Parent and Child Record in one dml statement.
I have written the code but the issue is it's inserting only parent record child record is not inserted.
public class ParentChildSample {
public static void InsertParentChild() {
Date dt = Date.today();
dt = dt.addDays(7);
List<Opportunity> opp = new List<Opportunity>();
List<Account> acc = new List<Account>();
for(Integer i = 0; i < 3; i++){
Opportunity newOpportunity = new Opportunity(Name='OpportunityWithAccountInsert ' +i,StageName='Prospecting',CloseDate=dt);
Account accountReference = new Account(MyExtID__c='SAP111111'+i);
newOpportunity.Account = accountReference;
Account parentAccount = new Account(Name='Hallie '+i);
system.debug('DDDDDDDDDDDDDDDDDD'+newOpportunity.Account);
opp.add(newOpportunity);
acc.add(parentAccount);
}
Database.SaveResult[] results = Database.insert(acc,opp);
for (Integer i = 0; i < results.size(); i++) {
if (results[i].isSuccess()) {
System.debug('Successfully created ID: '+ results[i].getId());
} else {
System.debug('Error: could not create sobject '+ 'for array element ' + i + '.');
System.debug(' The error reported was: '+ results[i].getErrors()[0].getMessage() + '\n');
}
}
}
}
In Developer guide they are using this particular line
Database.SaveResult[] results = Database.insert(new SObject[] {parentAccount, newOpportunity });
But that is for Single record so I'm Using the below line to Insert List of Parent and Child Record
Database.SaveResult[] results = Database.insert(acc,opp);
Database.insert()
method?