Please find the sample code below which creates a contact whenever a new account is created.Please tweak the below code as per your requirement which should do the trick for you.
trigger CreateAccountContact on Account (after insert, after update){
if(Trigger.isInsert){
List<Contact> ct = new List <Contact>();
for(Account acc : trigger.new){
Contact c = new Contact(LastName = acc.name,
AccountId=acc.id,
Fax=acc.Fax,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
/* similarly add all fields which you want */
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Phone=acc.Phone);
ct.add(c);
}
insert ct;
}
Hope this helps.
Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering similar issue.
hey i want to create account records when every lead converted but should automatically check for duplicates if it was duplicate account with "name" should update all the data in to old account record only instead of creating new account record
Please find the sample code below which creates a contact whenever a new account is created.Please tweak the below code as per your requirement which should do the trick for you. Hope this helps.
Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering similar issue.
Best Regards,
Nagendra.
Nagendra's code works for you if you replace the account fields with Contact fields and Contact fields with Account fields.
And, this trigger has to be a BEFORE INSERT trigger on Contact object.
Don't forget to update Contact.AccountID field, after Account creation.
if it was duplicate account with "name" should update all the data in to old account record only instead of creating new account record
AccountId is the lookup field in Contact. This is what links the contact to the account you are creating.
Try the trigger code below.
Let me know if this helps.
Can you ask your question in a separate thread?
We are happy to help you. But since your requirement is different from Abhishek's, it is better to keep them seprate.