User Story
As a sales representative for ABC Company, I want to ensure that any time a new Account record is created with an Industry of "Consulting" in Salesforce, an associated Contact record is automatically created with the Contact's Lastname set to the name of the Account and the Contact's phone number set to the phone number of the Account. This will help me save time and ensure that all necessary records are created in a timely and accurate manner.
Code
Apex Trigger
trigger CreateAssociatedContact on Account (after insert) {
List<Contact> newContacts = new List<Contact>();
for (Account newAccount : Trigger.new) {
if (newAccount.Industry == 'Consulting') {
Contact newContact = new Contact();
newContact.LastName = newAccount.Name;
newContact.Phone = newAccount.Phone;
newContact.AccountId = newAccount.Id;
newContacts.add(newContact);
}
}
insert newContacts;
}