+ Start a Discussion
Aishwarya P 4Aishwarya P 4 

Auto Create a contact when account is created

This is the controller class:

public class MYAPEX_Acc
{
    public static void createcon(List<Account> lstaccforcon)
    {
        List<Contact> lstcon = new List<Contact>();
        for(Account acc : lstaccforcon)
        {
        Contact newcon = new Contact();
        newcon.AccountId = acc.Id;
        newcon.FirstName = acc.Name;
        newcon.Description = acc.Description;
        newcon.Phone = acc.Phone;
        newcon.Email = acc.Email__c;
        lstcon.add(newcon);        
        }
        try{
        INSERT lstcon;
        }
        catch(exception e)
        {
        }
    }
}


This is the trigger :

trigger AishuAccount on Account (before insert,before update,after insert,after update) 
{
    List<Account> lstacc = new List<Account>();
    List<Account> lstaccforcon = new List<Account>();
    List<Contact> lstcon = new List<Contact>();

    If(Trigger.isAfter)
        {
        If(Trigger.isInsert)
        {
      
        for(Account acc : Trigger.new)
        {
        If(acc.Phone != NULL && acc.Name != NULL)
        lstaccforcon.add(acc);
        }
        }
       
        If(lstaccforcon.size() > 0)
        {
        MYAPEX_Acc.createcon(lstaccforcon);
        }
    }
    
}


I'm unable to create a contact with this code. please help
Best Answer chosen by Aishwarya P 4
sandip bijlwansandip bijlwan
LastName is mandatory field in Contact. please Add that field contact will be created.

All Answers

sandip bijlwansandip bijlwan
LastName is mandatory field in Contact. please Add that field contact will be created.
This was selected as the best answer
Shweta_AgarwalShweta_Agarwal
Hi Aishwarya,

In your code you missed adding Lastname in contact and its a required field. So in your class MYAPEX_Acc add below line

newcon.LastName = acc.Name;

Thanks
Shweta
Tejas KardileTejas Kardile
Hi Aishwarya,

Below is the working code:

public class MYAPEX_Acc {
    public static void createcon(List<Account> lstaccforcon)
        {
            List<Contact> lstcon = new List<Contact>();
            for(Account acc : lstaccforcon)
            {
            Contact newcon = new Contact();
            newcon.AccountId = acc.Id;
            newcon.FirstName = acc.Name;
            newcon.LastName = acc.Name;
            newcon.Description = acc.Description;
            newcon.Phone = acc.Phone;
            newcon.Email = acc.Email__c;
            lstcon.add(newcon);        
            }
            try{
            INSERT lstcon;
            }
            catch(exception e)
            {
            }
        }
}

trigger AishuAccount on Account (before insert,before update,after insert,after update) {
     List<Account> lstacc = new List<Account>();
    List<Account> lstaccforcon = new List<Account>();
    List<Contact> lstcon = new List<Contact>();

    If(Trigger.isAfter)
        {
        If(Trigger.isInsert)
        {
      
        for(Account acc : Trigger.new)
        {
      If(acc.Phone != NULL && acc.Name != NULL)
        lstaccforcon.add(acc);
        }
        }
       
        If(lstaccforcon.size() > 0)
        {
        MYAPEX_Acc.createcon(lstaccforcon);
        }
    }
}
 
Mahesh K 22Mahesh K 22
we can solve this senario with simple code by using triggers:
TRIGGER:


trigger accounttgr on account(after insert){
list<contact> cons = new list<contact>();
for(account a :trigger.new)

    contact c = new contact();
      c.lastname  = a.name;
      c.phone  = a.phone;
      c.email  = a.description
   slly we can match or assign the fileds which u want 
  finally 
     c.accountid = a.id;
    cons.add(c);
}
insert cons;
}
Aishwarya P 4Aishwarya P 4
Thank you.
Its working.