You need to sign in to do that
Don't have an account?
Michael Hedrick 2
Merge duplicate Leads and related records
Hello All,
I have been tasked to find duplicate Leads within our org.
Each Lead has an activity(s)
FInd the oldest lead and make this the master.
Take the activities from the other Leads and merge them to the Master Lead.
Delete the duplicate Leads.
This wil be a once and done since we now have a process to marry all the Leads together based on the email address.
I just need some help on the best approach to resolve this issue.
I do not actually need to merge the vlaues on the Lead details record as much as I need to make sure I get all of the Activities into a single Leads record.
I thought about trying the below approach but I am not thinking it will meet all of me needs
M
I have been tasked to find duplicate Leads within our org.
Each Lead has an activity(s)
FInd the oldest lead and make this the master.
Take the activities from the other Leads and merge them to the Master Lead.
Delete the duplicate Leads.
This wil be a once and done since we now have a process to marry all the Leads together based on the email address.
I just need some help on the best approach to resolve this issue.
I do not actually need to merge the vlaues on the Lead details record as much as I need to make sure I get all of the Activities into a single Leads record.
I thought about trying the below approach but I am not thinking it will meet all of me needs
List<AggregateResult> DuplicateList = [select count(email), id from lead where email != '' group by id having count(email) >1 limit 200]; For (AggregateResult Agm:DuplicateList){ String LeadEmail = string.valueOf(agm.get('email')); Lead ToLead = [Select id, email from Lead where email like :LeadEmail limit 1]; Lead FromLead = [Select id, email from Lead where Id != :ToLead.Id and email like :LeadEmail Limit 1]; system.debug(LoggingLevel.Info,'*** FromLead: ' + FromLead.email+' ('+FromLead.Id+')'); system.debug(LoggingLevel.Info,'*** ToLead : ' + ToLead.email+' ('+ToLead.Id+')'); database.merge(ToLead , FromLead ); }Thanks,
M
Could you try this code?
Hope you find this solution useful. If it does please mark as Best Answer to help others developers too.
Regards.
All Answers
Could you try this code?
Hope you find this solution useful. If it does please mark as Best Answer to help others developers too.
Regards.
This worked well thank you very much.
Just a note to others using the code you modify line 20 toif( leadsToMerge.size() > 1 )
and line 24 to Lead mergedLead = leadsToMerge[i];
I think I am going to add other related records form the leads like email results and opportunities and hopefully I can get the email and campaign information.
If I choose not to delete the merged leads and instead uncheck the flag on the new parent Lead(users concern with automatically deleting records) where in teh code are you determining the parent?
Also, there are over 800K leads. If I can only 50K records at a time I am assuming this would need to be edited to run as a batch ?
Thanks you,
M