+ Start a Discussion
Always ThinkinAlways Thinkin 

Merge a collection - how do you?

Hi,

I am trying to bulkify a trigger that merges incoming Leads based on a matched email so that the merge DML operation does not have to be executed in a for loop.

 

 

trigger LeadMerge on Lead (after insert) {
for (lead l: trigger.new){
try{
Lead mId = [select id from Lead where email = :l.email limit 1];
merge mId l.id;

 

 I can't figure out how to build a collection that can be used in the merge statement to return the pair of Lead IDs to be merged. If I build a map that holds the master ID and the matched record, how do I get the key-value pair to be returned in a way that the merge statement can process it?

 

Thanks for any help

 

Message Edited by Always Thinkin on 01-09-2010 05:08 PM
bob_buzzardbob_buzzard
According to the docs, the merge statement allows up to three records to be merged.  There's no mention of being able to carry out bulk merges of a collection of records.  So I think that even if you build your map structure to be processed outside the loop, you'd still have to process it via a loop. 
ahab1372ahab1372
another approach is not to use MERGE, but instead modify the existing leads (update fields with values from the new lead) and change the status of the new lead to something like "duplicate". If you are using assignemnet rules, you can easily set up a rule which routes these into a queue.