You need to sign in to do that
Don't have an account?
User 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;
}
//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;
}
Hi,
Please try this piece of code for your requirements.
//Trigger
//Trigger Handler
Hope it will help you.
If it help you mark as a best.
Regards,
Suraj
All Answers
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.
Hi,
Please try this piece of code for your requirements.
//Trigger
//Trigger Handler
Hope it will help you.
If it help you mark as a best.
Regards,
Suraj