function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shaanRocksshaanRocks 

Avoid duplaication using Merge Statement

Hi,

 

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 :

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_merge_statements.htm

 

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

 

Thanks in advance.

 

Regards,

shaan

 

 

bob_buzzardbob_buzzard

Can you post the error message?

shaanRocksshaanRocks

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.

bob_buzzardbob_buzzard

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

 

--- snip ---

 

 

Syntax:
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.

shaanRocksshaanRocks

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.

bob_buzzardbob_buzzard

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?

 

 

shaanRocksshaanRocks

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

bob_buzzardbob_buzzard

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.

 

 

shaanRocksshaanRocks

Thanks For the detailed description Bob.

 

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

 

Thanks

bob_buzzardbob_buzzard

No problem.  Just shout if you need more assistance.