+ Start a Discussion

Avoid duplaication using Merge Statement



Need to use Merge Functionality on a list of records of the same object inorder to avoid dupliaction of records.



How to use merge functionlity here:


List<SObject> LIstOfRecords = new List<SObject>();

List<SObject> ListToDelete= new List<SObject>();



Here LIstOfRecords contains all the records and ListToDelete records need to be merged into LIstOfRecords.


When used the MERGE Statement as:

Merge LIstOfRecords ListTo Delete.

It is throwing an error:


I have gone through the documentation :



Please let me know how to merge records of same object if the records are being duplicated.


Thanks in advance.







Can you post the error message?


Error: Compile Error: Merge requires a concrete SObject type: LIST at line 34 column 1


The Satatement used is:

Merge leadsToMerge leadsToDelete ;


Here leadsToMerge leadsToDelete are lists.


Looking at the docs, the merge statement expects a single master record and a list as parameters:


--- snip ---



merge sObject sObject
merge sObject sObject[]
merge sObject ID
merge sObject ID[]



--- snip ---


in each of the syntax entries, the first element is an sobject rather than a list.


HI Bob,

Thanks for Quick reply.

As per the doc we need to use single master record and multiple child records.


But here in my case:  

Please let me know how to merge records of same object if the records are being duplicated.


I have  a list of Records on an sobject  in that i have duplicate records on a field now i need to avoid duplication and merge the records accordingly based on conditions.

Please provide me with a solution i need to implement merge functionality on this list of records.


I'm not entirely clear about your requirement.  Is the list that you are processing entirely made up of duplicates of a single record, or are there multiples in there, some of which may be duplicated?




Yes in a list of records there can be multiple duplicate records.


That being the case, you'll have to provide most of the functionality yourself I'm afraid.


The way I've done this in the past is to create a Map of duplicates, where the key is a composite of the fields that make the record a duplicate and the value is a list of duplicate records.


E.g. if your leads are marked as duplicate if they have the same First and Last name, the key would be something like <FirstName>:<LastName>.  


You then iterate all your records, storing them in this map :-

look up the value associated with the composite first/last name

if there isn't a match create a new list and put it into the map

add the current record to the list


Once you have built the map, you can then create your non-duped list.  First instantiate a new list of leads, then iterate the map keyset and for each of them:


Get the list that is the value for the key

If there is only one element in the list then there isn't a dupe - simply add it to the new list of leads

If there are multiple elements, take the first element as the master, merge the others into it and add the master to the new list of leads


The new list of leads will then contain the de-duplicated leads.




Thanks For the detailed description Bob.


Will try to implement your logic and will let you know the result.




No problem.  Just shout if you need more assistance.