+ Start a Discussion
Abhishek Kumar 149Abhishek Kumar 149 

how to write trigger to create new account for the each new contact created

NagendraNagendra (Salesforce Developers) 
Hi Abhishek,

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.

Best Regards,
Nagendra.
 
LBKLBK
I guess Abhishek's requirement was to create an account when a new contact is created.

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.
Abhishek Kumar 149Abhishek Kumar 149
Can you please tell why there is need of updating AccountId and also give some brief about AccountId field. 
sales@myvarmasales@myvarma
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
LBKLBK
Hi Abhishek,

AccountId is the lookup field in Contact. This is what links the contact to the account you are creating.

Try the trigger code below.
 
trigger CreateNewAccount on Contact (before insert){
    List<Account> lstAcc = new List <Account>();
    Map<Id, Account> mapContactAcc = new Map<Id, Account>();
    for(Contact cnct : trigger.new){
        if(cnct.AccountId == null){
            Account objAcc = new Account(Name = cnct.LastName,
                        Fax=cnct.Fax,
                        BillingStreet=cnct.MailingStreet,
                        BillingCity=cnct.MailingCity,
                        /* similarly add all fields which you want */
                        BillingState=cnct.MailingState,
                        BillingPostalCode=cnct.MailingPostalCode,
                        BillingCountry=cnct.MailingCountry,
                        Phone=cnct.Phone);

            lstAcc.add(objAcc);
            mapContactAcc.put(cnct.Id, objAcc);
        }
    }
    insert lstAcc;

    //Link the account back to the Contact.
    for(Contact cnct : trigger.new){
        if(cnct.AccountId == null){
            cnct.AccountId = mapContactAcc.get(cnct.Id).Id;
        }
    }   
}
Let me know if this helps.

 
LBKLBK
Hi Pradeep,

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.