-1

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

1
  • 6
    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. Feb 7 '14 at 8:26
7
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; 
}
0
1

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.

0
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;
}
0
//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; 
    }
}
0
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;
}
1
  • 1
    insert conlst;?
    – martin
    Aug 12 '16 at 10:27
-2
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;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.