You need to sign in to do that
Don't have an account?
Lavanya 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.
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.
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.
-
Loop through your leads and create a set of contacts Ids.
-
Usre this set to query the contact records and store in a map.
-
Again loop through the leads, get the contact information from Map using map.get(contac.ConvertedContacId).
-
Create as many as contacts you want using the step 3.
-
Hold all the records in a list.
-
Insert list.
Make sure not to perform SOQL and DML in loop.{
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.