Create Related Contact on Account Creation

When a new Account is created, this trigger automatically generates a related Contact record, reducing manual effort and ensuring consistency between records.

Explore this concept further here: YouTube playlist.

Solution:

trigger AccountTrigger on Account (after insert) {

               if(Trigger.isInsert){ if(Trigger.isAfter){
                  AccountTriggerHandler.createContact(Trigger.New);
               }
     }
}
            public class AccountTriggerHandler {

                       public static void createContact(List<Account>
                           accList){ List<Contact> conList = new
                                            List<Contact>(); for(Account acc: accList){
                            Contact con= new Contact(); con.FirstName=
acc.Name + ‘FN’; con.LastName = acc.Name +
‘LN’; con.AccountId = acc.Id; conList.add(con);
               }
                          if(!conList.isEmpty()){
                                                       insert conList;
                             }
               }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)