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
harsha vardhan vasa 9harsha vardhan vasa 9 

Need to insert contact record for every account got created.

i need to inser contact record for every new account got created.
how can we achieve this and in how many ways we can achieve ?
please send me code snippiet also in programmatic approach.
could anyone help me on this.
Best Answer chosen by harsha vardhan vasa 9
SandhyaSandhya (Salesforce Developers) 
Hi,

You cane write trigger for that as below
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; 
}

You can also use process builder refer below link for the solution.

https://success.salesforce.com/answers?id=9063A000000iUqIQAU
 
Please mark it as Solved if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya
 
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

You cane write trigger for that as below
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; 
}

You can also use process builder refer below link for the solution.

https://success.salesforce.com/answers?id=9063A000000iUqIQAU
 
Please mark it as Solved if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya
 
 
This was selected as the best answer
Raj VakatiRaj Vakati
You no need to use trigger .. Simple you can use process builder to do this work and i would say use process builder rather than trigger 


https://automationchampion.com/2015/02/13/getting-started-with-process-builder-part-1-auto-create-a-record/
 
Ajay K DubediAjay K Dubedi
Hi Harsha,

Please refer the below code for default creation of contact on account creation with the use of trigger.

// Apex Class

public class CreateDefaultAccountContact {
    public static void createContact(List<Account> accountList)
    {
        List<Contact> contactList = new List<Contact>();
        
        for(Account acc_iteration: accountList)
        {
            Contact contactObject = new Contact();
            contactObject.LastName = 'Default Account Contact';
            contactObject.Email = 'xyz@yahoo.com';
            contactObject.Title = 'Account Contact';
            contactObject.AccountId = acc_iteration.Id;
            contactList.add(contactObject);
        }
        
        if(contactList.size()>0)
        {
            insert contactList;
        }
    }
}

// Trigger

trigger AccountDefaultContact on Account (after insert) {
    
    if(Trigger.IsInsert && Trigger.IsAfter)
    {
        CreateDefaultAccountContact.createContact(Trigger.New);
    }
}


Thank You,
Ajay Dubedi
harsha vardhan vasa 9harsha vardhan vasa 9
Thanks Sandhya for your time and effort :)