You need to sign in to do that
Don't have an account?

Urgent requirement Make this highlighted line Reusable in Apex Code
public class DuplicateLeadMergeHandler
{
public static void MergeLead(List<Lead> newLead)
{
Set<String> dupemails = new Set<String>();
Set<String> dupphones = new Set<String>();
Set<String> dupmobiles = new Set<String>();
Set<ID> leadID = new Set<ID>();
Map<String, Lead> masterLeadMap = new Map<String, Lead>();
for(Lead oldLead : newLead)
{
If(oldLead.Email != Null)
dupemails.add(oldLead.Email);
else
If(oldLead.Phone != Null)
dupphones.add(oldLead.Phone);
else
if(oldLead.MobilePhone != Null)
dupmobiles.add(oldLead.MobilePhone);
leadID.add(oldLead.id);
}
List<lead> oldLeadList = [SELECT Id, Name, OwnerId, MobilePhone, Email, Phone, Instagram_Handle__c, Website, Interested_Event_Locations__c, Vendor_Type__c, Items_Interested_in_Selling__c FROM Lead WHERE (Email IN: dupemails OR Phone IN: dupphones OR MobilePhone IN: dupmobiles) AND Id NOT IN: leadId AND IsConverted = false];
List<lead> newleadList = [SELECT Id, OwnerId, MobilePhone, Email, Phone, Instagram_Handle__c, Website, Interested_Event_Locations__c, Vendor_Type__c, Items_Interested_in_Selling__c FROM Lead WHERE (Email IN: dupemails OR Phone IN: dupphones OR MobilePhone IN: dupmobiles) AND Id IN: leadId AND IsConverted = false];
If(!oldLeadList.IsEmpty())
{
for(Lead oldLead: newLeadList)
{
If(oldLead.Email != Null)
masterLeadMap.put(oldLead.Email, oldLead);
else
If(oldLead.Phone != Null)
masterLeadMap.put(oldLead.Phone, oldLead);
else
if(oldLead.MobilePhone != Null)
masterLeadMap.put(oldLead.MobilePhone, oldLead);
}
If(masterLeadMap.size() > 0 && masterLeadMap != null)
{
Lead masterLead = new Lead();
for(Lead oldLead : oldLeadList)
{
If(masterLeadMap.containskey(oldLead.Email))
{
masterLead = masterLeadMap.get(oldLead.Email);
If(masterLead != Null)
{
Task leadTask = new Task();
leadTask.WhoId = masterLead.Id;
leadTask.OwnerId = masterLead.OwnerId;
leadTask.Subject = 'Matched Lead - Follow Up';
leadTask.Status = 'In Progress';
leadTask.Priority = 'High';
leadTask.Description = 'Old Matched Lead Info \nLead Record Id :- '+oldLead.Id+'\nLead Name :- '+oldLead.Name+'\nEmail Address :- '+oldLead.Email+'\nMobile Phone :- '+oldLead.MobilePhone+'\nInstagram Handle :- '+oldLead.Instagram_Handle__c+'\nWebsite :- '+oldLead.Website+'\nInterested Event Locations :- '+oldLead.Interested_Event_Locations__c+'\nVendor Type :- '+oldLead.Vendor_Type__c+'\nItems Interested In Selling :- '+oldLead.Items_Interested_in_Selling__c+'';
insert leadTask;
System.debug(masterLead);
Database.MergeResult results = Database.Merge(masterLead, oldLead, false);
System.debug(results);
}
}
Else
If(masterLeadMap.containskey(oldLead.Phone))
{
masterLead = masterLeadMap.get(oldLead.Phone);
If(masterLead != Null)
{
Task leadTask = new Task();
leadTask.WhoId = masterLead.Id;
leadTask.OwnerId = masterLead.OwnerId;
leadTask.Subject = 'Matched Lead - Follow Up';
leadTask.Status = 'In Progress';
leadTask.Priority = 'High';
leadTask.Description = 'Old Matched Lead Info \nLead Record Id :- '+oldLead.Id+'\nLead Name :- '+oldLead.Name+'\nEmail Address :- '+oldLead.Email+'\nMobile Phone :- '+oldLead.MobilePhone+'\nInstagram Handle :- '+oldLead.Instagram_Handle__c+'\nWebsite :- '+oldLead.Website+'\nInterested Event Locations :- '+oldLead.Interested_Event_Locations__c+'\nVendor Type :- '+oldLead.Vendor_Type__c+'\nItems Interested In Selling :- '+oldLead.Items_Interested_in_Selling__c+'';
insert leadTask;
System.debug(masterLead);
Database.MergeResult results = Database.Merge(masterLead, oldLead, false);
}
Else
If(masterLeadMap.containskey(oldLead.MobilePhone))
{
masterLead = masterLeadMap.get(oldLead.MobilePhone);
If(masterLead != Null)
{
Task leadTask = new Task();
leadTask.WhoId = masterLead.Id;
leadTask.OwnerId = masterLead.OwnerId;
leadTask.Subject = 'Matched Lead - Follow Up';
leadTask.Status = 'In Progress';
leadTask.Priority = 'High';
leadTask.Description = 'Old Matched Lead Info \nLead Record Id :- '+oldLead.Id+'\nLead Name :- '+oldLead.Name+'\nEmail Address :- '+oldLead.Email+'\nMobile Phone :- '+oldLead.MobilePhone+'\nInstagram Handle :- '+oldLead.Instagram_Handle__c+'\nWebsite :- '+oldLead.Website+'\nInterested Event Locations :- '+oldLead.Interested_Event_Locations__c+'\nVendor Type :- '+oldLead.Vendor_Type__c+'\nItems Interested In Selling :- '+oldLead.Items_Interested_in_Selling__c+'';
insert leadTask;
System.debug(masterLead);
Database.MergeResult results = Database.Merge(masterLead, oldLead, false);
System.debug(results);
}
}
}
}
}
}
}
{
public static void MergeLead(List<Lead> newLead)
{
Set<String> dupemails = new Set<String>();
Set<String> dupphones = new Set<String>();
Set<String> dupmobiles = new Set<String>();
Set<ID> leadID = new Set<ID>();
Map<String, Lead> masterLeadMap = new Map<String, Lead>();
for(Lead oldLead : newLead)
{
If(oldLead.Email != Null)
dupemails.add(oldLead.Email);
else
If(oldLead.Phone != Null)
dupphones.add(oldLead.Phone);
else
if(oldLead.MobilePhone != Null)
dupmobiles.add(oldLead.MobilePhone);
leadID.add(oldLead.id);
}
List<lead> oldLeadList = [SELECT Id, Name, OwnerId, MobilePhone, Email, Phone, Instagram_Handle__c, Website, Interested_Event_Locations__c, Vendor_Type__c, Items_Interested_in_Selling__c FROM Lead WHERE (Email IN: dupemails OR Phone IN: dupphones OR MobilePhone IN: dupmobiles) AND Id NOT IN: leadId AND IsConverted = false];
List<lead> newleadList = [SELECT Id, OwnerId, MobilePhone, Email, Phone, Instagram_Handle__c, Website, Interested_Event_Locations__c, Vendor_Type__c, Items_Interested_in_Selling__c FROM Lead WHERE (Email IN: dupemails OR Phone IN: dupphones OR MobilePhone IN: dupmobiles) AND Id IN: leadId AND IsConverted = false];
If(!oldLeadList.IsEmpty())
{
for(Lead oldLead: newLeadList)
{
If(oldLead.Email != Null)
masterLeadMap.put(oldLead.Email, oldLead);
else
If(oldLead.Phone != Null)
masterLeadMap.put(oldLead.Phone, oldLead);
else
if(oldLead.MobilePhone != Null)
masterLeadMap.put(oldLead.MobilePhone, oldLead);
}
If(masterLeadMap.size() > 0 && masterLeadMap != null)
{
Lead masterLead = new Lead();
for(Lead oldLead : oldLeadList)
{
If(masterLeadMap.containskey(oldLead.Email))
{
masterLead = masterLeadMap.get(oldLead.Email);
If(masterLead != Null)
{
Task leadTask = new Task();
leadTask.WhoId = masterLead.Id;
leadTask.OwnerId = masterLead.OwnerId;
leadTask.Subject = 'Matched Lead - Follow Up';
leadTask.Status = 'In Progress';
leadTask.Priority = 'High';
leadTask.Description = 'Old Matched Lead Info \nLead Record Id :- '+oldLead.Id+'\nLead Name :- '+oldLead.Name+'\nEmail Address :- '+oldLead.Email+'\nMobile Phone :- '+oldLead.MobilePhone+'\nInstagram Handle :- '+oldLead.Instagram_Handle__c+'\nWebsite :- '+oldLead.Website+'\nInterested Event Locations :- '+oldLead.Interested_Event_Locations__c+'\nVendor Type :- '+oldLead.Vendor_Type__c+'\nItems Interested In Selling :- '+oldLead.Items_Interested_in_Selling__c+'';
insert leadTask;
System.debug(masterLead);
Database.MergeResult results = Database.Merge(masterLead, oldLead, false);
System.debug(results);
}
}
Else
If(masterLeadMap.containskey(oldLead.Phone))
{
masterLead = masterLeadMap.get(oldLead.Phone);
If(masterLead != Null)
{
Task leadTask = new Task();
leadTask.WhoId = masterLead.Id;
leadTask.OwnerId = masterLead.OwnerId;
leadTask.Subject = 'Matched Lead - Follow Up';
leadTask.Status = 'In Progress';
leadTask.Priority = 'High';
leadTask.Description = 'Old Matched Lead Info \nLead Record Id :- '+oldLead.Id+'\nLead Name :- '+oldLead.Name+'\nEmail Address :- '+oldLead.Email+'\nMobile Phone :- '+oldLead.MobilePhone+'\nInstagram Handle :- '+oldLead.Instagram_Handle__c+'\nWebsite :- '+oldLead.Website+'\nInterested Event Locations :- '+oldLead.Interested_Event_Locations__c+'\nVendor Type :- '+oldLead.Vendor_Type__c+'\nItems Interested In Selling :- '+oldLead.Items_Interested_in_Selling__c+'';
insert leadTask;
System.debug(masterLead);
Database.MergeResult results = Database.Merge(masterLead, oldLead, false);
System.debug(results);
}}
Else
If(masterLeadMap.containskey(oldLead.MobilePhone))
{
masterLead = masterLeadMap.get(oldLead.MobilePhone);
If(masterLead != Null)
{
Task leadTask = new Task();
leadTask.WhoId = masterLead.Id;
leadTask.OwnerId = masterLead.OwnerId;
leadTask.Subject = 'Matched Lead - Follow Up';
leadTask.Status = 'In Progress';
leadTask.Priority = 'High';
leadTask.Description = 'Old Matched Lead Info \nLead Record Id :- '+oldLead.Id+'\nLead Name :- '+oldLead.Name+'\nEmail Address :- '+oldLead.Email+'\nMobile Phone :- '+oldLead.MobilePhone+'\nInstagram Handle :- '+oldLead.Instagram_Handle__c+'\nWebsite :- '+oldLead.Website+'\nInterested Event Locations :- '+oldLead.Interested_Event_Locations__c+'\nVendor Type :- '+oldLead.Vendor_Type__c+'\nItems Interested In Selling :- '+oldLead.Items_Interested_in_Selling__c+'';
insert leadTask;
System.debug(masterLead);
Database.MergeResult results = Database.Merge(masterLead, oldLead, false);
System.debug(results);
}
}
}
}
}
}
}
This can be called as follows:
All Answers
I want to create a method for the following highlighted part and use it inside the IF the loops
I'm new to this please help if possible]
thanks
Doctor Strange
This can be called as follows:
Thanks for your efforts
actually, I figured it out early and wanted to provide the best solution for future reference
as you took so much effort solving this for me im marking your solution as the best solution.
Thanks & Regards
Doctor Strange