User Story
As a sales representative for ABC Company, I want to be able to quickly create multiple Contact records for each new Account record, based on the number of contacts specified in a "Number of Contacts" field on the Account Object. To achieve this, I would like the system to automatically create a corresponding number of Contact records whenever a new Account record is created or updated. This will help me save time and ensure that all necessary records are created in a timely and accurate manner.
Code
Apex Trigger
trigger CreateMultipleContacts on Account (after insert, after update) {
List<Contact> newContacts = new List<Contact>();
for (Account newAccount : Trigger.new) {
if (newAccount.Number_of_Contacts__c > 0) {
Integer numberOfContacts = newAccount.Number_of_Contacts__c;
for (Integer i = 0; i < numberOfContacts; i++) {
Contact newContact = new Contact();
newContact.LastName = newAccount.Name;
newContact.AccountId = newAccount.Id;
newContacts.add(newContact);
}
}
}
insert newContacts;
}