Write a trigger that automatically create a contact when account is created

  • Suppose i am creating a account name 'srsahoo',then automatically a contact is created associated with srsahoo.

    Did you already try something yourself ? This place is not intended to have people do your work. We're here to help you when you get stuck doing it.

  • 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 do it easily through Process builder. No need to write trigger at all. It's better to avoid code if you can do it through cutomization.

  • Trigger accountinsertautomaticallycontactalsoinsert on Account(After Insert){
        List<Contact> conlst = new List<Contact>();
        For(Account acc:Trigger.New){
           Contact con = New Contact();
           con.lastname = acc.name;
           con.accountid=acc.id;
           conlst.add(con);
        }
        insert conlst;
    }
    

    `insert conlst;`?

  • trigger InsertContact on Account (after insert)
    {
        List <Contact> cntLst = new List<Contact>();
            for(Account acc: Trigger.New)
            {
                Contact cnt = new Contact();
                cnt.LastName = 'chirag'+''+ acc.name;
                 cnt.AccountId=acc.id;
                cntLst.Add(cnt);
             }
                 insert cntLst;
    }
    
  • //Trigger
    
    Trigger CreateContact on Account (after insert) {
        if(Trigger.isInsert) {
            AccountTriggerHandler.createContact(Trigger.new);
        }
    }
    
    
    // Handler Class
    
    public class AccountTriggerHandler {
        public static void createContact(List<Account> accounts) {
            List<Contact> contact = new List<Contact>();
            for(Account acc : accounts){
                Contact con = new Contact(LastName = acc.name,
                            AccountId=acc.id,
                            MailingStreet=acc.BillingStreet,
                            MailingCity=acc.BillingCity,
                            MailingState=acc.BillingState,
                            MailingPostalCode=acc.BillingPostalCode,
                            MailingCountry=acc.BillingCountry,
                            Fax=acc.Fax);
                contact.add(con);
            }
            insert contact; 
        }
    }
    
  • trigger ConOnNewAcc on Account (after insert){
        Public List<Contact> con = new List<Contact>();
        For(Account Acc:Trigger.new ){ 
            Contact c=new contact();
            c.accountid=acc.id;
            c.Lastname=acc.Name; 
            c.phone=Acc.phone;
            c.mailingcity=acc.Billingcity;
            con.add(c);
        } 
        Insert Con;
    }
    

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM