-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This test utility class contains one static method, createAccountsWithOpps(), which accepts the number of accounts (held in the numAccts parameter) and the number of related opportunities to create for each account (held in the numOppsPerAcct parameter). The first loop in the method creates the specified number of accounts and stores them in the accts list variable. After the first loop, the insert() DML statement is called to create all accounts in the list in the database.
The second loop creates the opportunities. Because each group of opportunities are linked to one account, the outer loop iterates through accounts and contains a nested loop that creates related opportunities for the current account. The next time the nested loop is run, opportunities are added to the same list using the add() method. Opportunities are linked to their parent accounts using the AccountId field. The total number of all opportunities that are created is the product of the number of opportunities with the number of accounts (numOppsPerAcct*numAccts). Next, the insert() DML statement is efficiently called outside the loop to create all opportunities in the collection for all accounts in one call only.
Finally, this method returns a list of the new accounts.