+ Start a Discussion
Lavanya Ponniah 3Lavanya Ponniah 3 

How to create multiple contacts when we convert a lead?

trigger LeadConvert on Lead (before update) 
{
    List<Account> acc = new List<Account>();
    Integer listint = 5;
   
    List<Opportunity> op = new List<Opportunity>();
    List<Contact> con = new List<Contact>();
   
    for(Lead l : Trigger.New)
    {     
       Lead oldl = Trigger.oldMap.get(l.Id);
        if (oldl.IsConverted == false && l.isConverted == true) 
        {     
            if (l.ConvertedContactId != null) 
            {
                List<Contact> contCk = [Select Id, LastName From Contact Where Id =: l.ConvertedContactId]; 
                   conCk.LastName = l.lastName;
                    con1.add(conCk); 
                    con2.add(conCk);
                    System.debug('After insert con2'+con2);
                    con3.add(conCk);
                    System.debug('After insert con3'+con3);

                }
                
            }
            if (l.ConvertedAccountId != null)
            {
                Account accCk = [Select Id, Name From Account Where Id =: l.ConvertedAccountId];
                accCk.Name = l.lastName;             
                acc.add(accCk);
            }
            if (l.ConvertedOpportunityId != null)
            {
                Opportunity oppCk = [Select Id, Name From Opportunity Where Id =: l.ConvertedOpportunityId];
                oppCk.Name = l.lastName;
                op.add(oppCk);
            }
        }
    }
This is my trigger in this it only creates one Contact but, i need to create multiple contacts when we convert lead. 
AshlekhAshlekh
Hi,

First of that your are using soql in for loop which is not good practise.

Second I am not able to get con1 and con2 and con3 variable, where you have declare this.

Lavanya Ponniah 3Lavanya Ponniah 3
Ok,Now you check this.
trigger LeadConvert on Lead (before update)
{
    List<Account> acc = new List<Account>();
    
    List<Opportunity> op = new List<Opportunity>();
    List<Contact> con1 = new List<Contact>();
    List<Contact> con2 = new List<Contact>();
    List<Contact> con3 = new List<Contact>();
   
    for(Lead l : Trigger.New)
    {    
       Lead oldl = Trigger.oldMap.get(l.Id);
        if (oldl.IsConverted == false && l.isConverted == true)
        {    
            if (l.ConvertedContactId != null)
            {
                for(Contact conCk : [Select Id, LastName From Contact Where Id =: l.ConvertedContactId])
                {
                 conCk.LastName = l.lastName;
                    con1.add(conCk);
                    System.debug('After insert con1'+con1);
                    con2.add(conCk);
                    System.debug('After insert con2'+con2);
                    con3.add(conCk);
                    System.debug('After insert con3'+con3);   
                    update conCk;
                }
               
            }
               
            if (l.ConvertedAccountId != null)
            {
                Account accCk = [Select Id, Name From Account Where Id =: l.ConvertedAccountId];
                accCk.Name = l.lastName;            
                acc.add(accCk);
            }
            if (l.ConvertedOpportunityId != null)
            {
                Opportunity oppCk = [Select Id, Name From Opportunity Where Id =: l.ConvertedOpportunityId];
                oppCk.Name = l.lastName;
                op.add(oppCk);
            }
        }
    }
}

Can you modified based on my question.
Bhawani SharmaBhawani Sharma
  1. Loop through your leads and create a set  of contacts Ids. 
  2. Usre this set to query the contact records and store in a map.
  3. Again loop through the leads, get the contact information from Map using map.get(contac.ConvertedContacId).
  4. Create as many as contacts you want using the step 3.
  5. Hold all the records in a list.
  6. Insert list.
Make sure not to perform SOQL and DML in loop.
Lavanya Ponniah 3Lavanya Ponniah 3
trigger LeadConvert on Lead (after insert,after update)
{
    set<Id> contact1 = new Set<Id>();
    Set<Id> contact2 = new Set<Id>();
    Set<Id> contact3 = new Set<Id>();
    Set<Id> contact4 = new Set<Id>();
    Set<Id> accountIds = new Set<Id>();
    Set<Id> opp = new Set<Id>();
   
for(lead l : trigger.new)
    {
     contact1.add(l.ConvertedContactId);
        contact2.add(l.ConvertedContactId);
        contact3.add(l.ConvertedContactId);
        contact4.add(l.ConvertedContactId);
        accountIds.add(l.ConvertedAccountId);
        opp.add(l.ConvertedOpportunityId);
    }
   
    Map<id, Contact> con1 = new Map<id, Contact>([Select Id, LastName From Contact Where Id in : contact1]);
    Map<id, Contact> con2 = new Map<id, Contact>([Select Id, LastName From Contact Where Id in : contact2]);
    Map<id, Contact> con3 = new Map<id, Contact>([Select Id, LastName From Contact Where Id in : contact3]);
    Map<id, Contact> con4 = new Map<id, Contact>([Select Id, LastName From Contact Where Id in : contact4]);
    Map<id, Account> acc = new Map<id, Account>([Select Id, Name From Account Where Id in : accountIds]);
    Map<id, Opportunity> op = new Map<id, Opportunity>([Select Id, Name From Opportunity Where Id in : opp]);

    for(Lead l : Trigger.New)
    {    
       Lead oldl = Trigger.oldMap.get(l.Id);
        if (oldl.IsConverted == false && l.isConverted == true  )
        {   
            if (l.ConvertedContactId != null || l.LastName == null )
            {
                List<Contact> c1 = con1.get(l.ConvertedContactId);
                List<Contact> c2 = con2.get(l.ConvertedContactId);
                List<Contact> c3 = con3.get(l.ConvertedContactId);
                List<Contact> c4 = con4.get(l.ConvertedContactId);               
            }
               
            if (l.ConvertedAccountId != null)
            {
    List<Account> a = acc.get(l.ConvertedAccountId);               
            }
            if (l.ConvertedOpportunityId != null)
            {
                List<Opportunity> o = op.get(l.ConvertedOpportunityId);
            }
        }
    }
    if(a.size() > 0)
    {
        insert a;
    }
    if (o.size() > 0)
    {
  insert o;
}

if (c1.size() > 0)
    {
  insert c1;
}
    if (c2.size() > 0)
    {
  insert c2;
}
    if (c3.size() > 0)
    {
  insert c3;
}
    if (c4.size() > 0)
    {
  insert c4;
}
}

Like this u r telling.
Deepak Kumar ShyoranDeepak Kumar Shyoran
As when you convert a lead it'll automatically insert/create an account and a Contact, so just let us know how many additional Contact and Account you want to create more and what will be the LastName for new Contact and Name of Account for additional Account and Contact you want to insert. So that we can help you more the code written by you above is full of errors.