User Story
Create a new Apex class named "Class2481" within your Salesforce development environment.
Implement a method named generateAccounts
within your class with the following signature:
public static void generateAccounts(String rating, Integer repetition, Boolean createContacts);
This method should create a specified number of accounts with the given rating. If createContacts
is set to true, it should also create associated contacts for each account.
Codes
public class DMLPractice {
public static void createAccounts(String rating, Integer repetition, Boolean createContacts) {
List<Account> accountList = new List<Account>();
List<Contact> contactList = new List<Contact>();
for (Integer i = 1; i <= repetition; i++) {
Account acc = new Account();
acc.Name = 'Test' + i;
acc.Active__c = 'Yes';
acc.Rating = rating;
accountList.add(acc);
}
Insert accountList;
if (createContacts) {
Integer contactCounter = 1;
for (Account acc : accountList) {
Contact con = new Contact();
con.LastName = 'Test' + contactCounter;
con.AccountId = acc.Id;
contactList.add(con);
contactCounter++;
}
if (contactList.size() != 0) {
Insert contactList;
}
}
}
}