You need to sign in to do that
Don't have an account?
Convert a Lead to Multiple Contacts
Hello! I cracked this last week and wanted to share my working code with everyone. This is for a scenario when you have multiple "contacts" or contact data on the lead object that will need to become multiple contacts. For this one, we had 3 sections on the page where we were capturing data for brokers, Owners, ect. This code takes each of those sections and creates a new contact for each section. feel free to comment with any suggestions to enhance or if you have any addition questions about how it is setup. Thanks!
Trigger
trigger UpdateContactObject_Trigger on Lead (before update)
{/*
* Create variables to store the lead values,
* new and old. This will be used below to
* determine if the leads are being converted,
* not converted yet, or has already been
* converted.
*/
List<Lead> newLeads = trigger.new;
Map<Id, Lead> mOldLeads = trigger.oldMap;
Lead oldLead;
/*
* Create sets of Ids to store the records
* associated with the converted leads
*/
Set<Id> convertedAccountIds = new Set<Id>();
Set<Id> convertedContactIds = new Set<Id>();
Set<Id> convertedOpportunityIds = new Set<Id>();
/*
* Loop through the leads submitted through this
* trigger. Populate the appropriate sets of Ids
* for each lead with populated values.
*/
for (Lead l : newLeads) {
if (l.convertedAccountId != null) {
convertedAccountIds.add(l.convertedAccountId);
}
if (l.convertedContactId != null) {
convertedContactIds.add(l.convertedContactId);
}
}
Account accounts =
[SELECT Id, Name
FROM Account
WHERE Id IN : convertedAccountIds];
for(Lead l : Trigger.New){
If(l.IsConverted){
Contact c1=New Contact(LastName=l.True_Owner_Last_Name__c,FirstName=l.True_Owner_First_Name__c,
Phone=l.True_Owner_Phone__c,MailingStreet=l.True_Owner_Address__c,MailingState=l.True_Owner_State__c,MailingCity=l.True_Owner_City__c,
MailingPostalCode=l.True_Owner_Zip__c,Email=l.True_Owner_Email__c, AccountId=accounts.id);
insert c1;
Contact c2=New Contact(LastName=l.Broker_Last_Name__c,FirstName=l.Owner_First_Name__c,
Phone=l.Owner_Phone__c,MailingStreet=l.Owner_Address__c,MailingState=l.Owner_State__c,MailingCity=l.Owner_City__c,
MailingPostalCode=l.Owner_Zip_Code__c,Email=l.Owner_Email__c,AccountId=accounts.id);
insert c2;
Contact c3=New Contact(LastName=l.Owner_Last_Name__c,FirstName=l.Broker_First_Name__c,
Phone=l.Broker_Phone__c,MailingStreet=l.Broker_Address__c,MailingState=l.Broker_State__c,MailingCity=l.Broker_City__c,
MailingPostalCode=l.Broker_Zip_Code__c,Email=l.Broker_Email__c,AccountId=accounts.id);
insert c3;
}}}
Coverage:
@isTest
public class UpdateContactObject_TriggerTestClass{
Static testmethod void UpdateContactObject_TriggerTestClass(){
//insert Lead
Lead Lead = new Lead(
FirstName='test ',
LastName='Test2',
Company='test',
Owner__c='test',
True_Owner__c='test3',
Broker__c='test4'
);
insert Lead;
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(lead.id);
lc.setDoNotCreateOpportunity(false);
lc.setConvertedStatus('Qualified');
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}}
Thanks,
Pratik