function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
User One 35User One 35 

I need to create a trigger with handler class to automatically create an Contact when a new account is created

I am trying to create a Account record when the account record is saved automatically create a contact record. I tried with only trigger its working but I want to write trigger and handler class  but its not working 

//Tigger

trigger AccountContact on Account(after insert, after update) {
        if(Trigger.isInsert){
                 AccountTriggerHandler.createContact(Trigger.new);
     }
  }

// Handler Class 
Public class TriggerHandler{
           List<Contact> ct = new List <Contact>();
               for(Account acc : trigger.new){
                     Contact c = new Contact(LastName = acc.name,
                                                               AccountId=acc.id,
                                                               Fax=acc.Fax,
                                                               Phone=acc.Phone);
        ct.add(c);
    }
    insert ct;
}
Best Answer chosen by User One 35
Suraj TripathiSuraj Tripathi

Hi,

Please try this piece of code for your requirements.

//Trigger
 

trigger AccountContact on Account(after insert) {
        if(Trigger.isInsert){
            AccountTriggerHandler.createContact(Trigger.new);
     }
 }

//Trigger Handler

Public class AccountTriggerHandler{
	public static void createContact(List<Account> accList){
		List<Contact> ct = new List <Contact>();
		for(Account acc : accList){
            Contact c = new Contact();
			c.LastName = acc.Name;
            c.AccountId= acc.Id;                                                 
            c.Fax=acc.Fax;                                               
            c.Phone=acc.Phone;                                               
			ct.add(c);
		}
		insert ct;
	}
}


Hope it will help you.

If it help you mark as a best.

Regards,

Suraj

All Answers

Ryan GreeneRyan Greene
Not sure why you would create two codes to solve this problem. I would just use the Trigger to create the Contact: something like this:
trigger AccountContact on Account(after insert, after update) {
     List<Contact> ct = new List <Contact>();
     if(Trigger.isInsert){
           Account acc : trigger.new;
           Contact c = new Contact;
   c.LastName = acc.name;
   c.AccountId=acc.id;
   c.Fax=acc.Fax;
   c.Phone=acc.Phone;
   ct.add(c);
    }
    insert ct;
}

 
User One 35User One 35
Hi Ryan
Thank You for repalying.

Yeah I have already tried that its working, but I want to try in different way because I am fresher exploring more in different ways.
Suraj TripathiSuraj Tripathi

Hi,

Please try this piece of code for your requirements.

//Trigger
 

trigger AccountContact on Account(after insert) {
        if(Trigger.isInsert){
            AccountTriggerHandler.createContact(Trigger.new);
     }
 }

//Trigger Handler

Public class AccountTriggerHandler{
	public static void createContact(List<Account> accList){
		List<Contact> ct = new List <Contact>();
		for(Account acc : accList){
            Contact c = new Contact();
			c.LastName = acc.Name;
            c.AccountId= acc.Id;                                                 
            c.Fax=acc.Fax;                                               
            c.Phone=acc.Phone;                                               
			ct.add(c);
		}
		insert ct;
	}
}


Hope it will help you.

If it help you mark as a best.

Regards,

Suraj

This was selected as the best answer