Code Snippet to Insert Bulk Accounts, add 5 Contacts to Each Account, and 2 Opportunity for each Contact
Execute the below script in developer console. It will create 5 Accounts records and each Account have 5 Contacts and each Contact will have 2 Opportunities.
Set<Id> conIds = new Set<Id>();
For(integer i=0; i<1; i++) {
Account acc = new Account(Name='Bulk Account '+i, AccountNumber='919102'+i, Rating='Hot');
Insert acc;
For(integer j=0; j<3; j++) {
Contact con = new Contact(LastName=acc.Name +' Contact '+j, AccountId=acc.Id);
Insert con;
conIds.add(con.Id);
}
}
List<Contact> lstContact = [Select Id, Name From Contact where Id in: conIds];
for(Contact con: lstContact) {
for(integer i=0; i<2; i++) {
Opportunity oppr = new Opportunity(Name=con.Name + ' Oppor '+ i, CloseDate=System.Today() + 12,StageName='Qualification', ContactId=con.id);
Insert oppr;
}
} About the original script
The title says 5 Accounts, 5 Contacts per Account and 2 Opportunities per Contact, but the code in the loops creates only 1 Account and 3 Contacts. It also runs an insert inside every loop and then queries the Contacts again just to get their names.
Script that matches the title (bulk-safe)
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 5; i++) {
accounts.add(new Account(Name = 'Bulk Account ' + i, AccountNumber = '919102' + i, Rating = 'Hot'));
}
insert accounts;
List<Contact> contacts = new List<Contact>();
for (Account acc : accounts) {
for (Integer j = 0; j < 5; j++) {
contacts.add(new Contact(LastName = acc.Name + ' Contact ' + j, AccountId = acc.Id));
}
}
insert contacts;
List<Opportunity> opportunities = new List<Opportunity>();
for (Contact con : contacts) {
for (Integer k = 0; k < 2; k++) {
opportunities.add(new Opportunity(
Name = con.LastName + ' Oppor ' + k,
CloseDate = System.today().addDays(12),
StageName = 'Qualification',
AccountId = con.AccountId,
ContactId = con.Id
));
}
}
insert opportunities; How it works
- It creates 5 Accounts and saves them with one insert.
- It creates 5 Contacts for each saved Account, 25 in total, and saves them with one insert.
- It creates 2 Opportunities for each saved Contact, 50 in total, and saves them with one insert. The Contact LastName is used for the name because no first name is set, so no extra query is needed.
That is 80 records created with only 3 DML statements. The original approach would need 5 + 25 + 50 = 80 statements and would hit the limit of 150 quickly if you raised the numbers.
How to check and clean up
Open any of the new Accounts and check its Contacts and Opportunities related lists. To remove the data, run delete [SELECT Id FROM Account WHERE Name LIKE 'Bulk Account%'];. The Contacts and Opportunities linked to those Accounts are deleted with them. Run this in a sandbox or developer org.
FAQ
Why set AccountId on the Opportunity?
It links the Opportunity to the Account, so it shows in the Account related list and is removed with it.
Can I change the numbers?
Yes. Change the loop limits. Keep the total below 10,000 DML rows per transaction.