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
sandeep kumarsandeep kumar 

How to use Merge call in apex class

I want to create the functionality "Find Duplicate" as is avaialable in Lead for Person Accounts. The button will be on the Page layout of the Account page and will be available only for Person Accounts.
The functionaly needs to be developed in Visual Force and Apex. Can I use the apex merge call in an apex class. Will this be a better idea to create this in apex rather than as an S-Control. Has anybody tried this or has some piece of code that can get me working
would be grateful

Hi sandeep -


Were you able to merge the records using Apex? If so , can you please post some sample code of what you did in Apex? I am also trying to do merge in Apex




The merge call allows you to combine multiple records (up to 3 at once). To use the call, follow these steps:


1) Place the three records into a list. The "surviving" record should be in index 0.


2) Move any field data from the records in index 1 and/or 2, either through custom logic or user intervention.


3) Call the Merge statement, passing it the list of up to 3 records.


4) The record in index 0 will have the new data, while the records in the other two indices will be deleted.


CAn you call merge statement on a batch of records?

What happens if I have 100 leads I want to merge? If I have to call one at a time, it will hit governor limits?




As of Spring '11, the limit is tied to DML operation limits, so presumably merging 100 leads in a single transaction shouldn't be an issue. In fact, you should be able to merge up to 450 leads (assuming 3 per merge) per transaction.


Hi Can you please post sample code  as it would be easy to understand.



The DML Limits seems to be increased to 150 as of Spring 11.


However,. a merge operation might trigger other DML statements - you never know what other packages are installed and what they do with leads.

In my case,. at some number less than 150,. I hit. so I am playing it safe and doing it for less than 50 leads.


I wrote a batch apex,. if I have more than 50 leads.







Well I ran into another limit - the leadscoring package (which I have installed as well) incorrectly calls a @future method. So run into this limit right after 10 lead merges! Interesting,. since the batch apex limit for future calls is 0, the bug is not hit



Seb OrtizSeb Ortiz

There's one thing I don't get.

If I merge TargetLead Lead1 Lead2 where TargetLead is the one wich will survive, then any addtional info in Lead1 & Lead2 that I want to keep I should manually copy it to TargetLead or the merge opration takes care of it?






You prepare your targetLead record with all the right fields.

The only thing merge is really doing is deleting the two leads and if there was anything pointing to them through a look-up, making it point to your target lead.



Another point to note is that re-parenting (i.e making everything point to targetLead) does not change ownership.If targetLead and Lead1 have different owners, and something pointing to Lead1 was owned by same person as Lead1, the new owner won't be automatically the owner of targetLead. You will have to write code to do that.




Seb OrtizSeb Ortiz

It's clear now. Thank you very much


how to use merge call for custom objects in apex class?


Please share the sample code for merging the accounts in salesforce by using apex.


@Rocks1, In the future, please use a new thread instead of reviving old threads. You'll have a better chance of your message being seen.


Account masterAccount = ..., slaveAccount = ...;
merge masterAccount slaveAccount;

When the operation is successful, masterAccount will remain, and slaveAccount will be deleted, with all of slaveAccount's detail records being moved permanently to masterAccount. slaveAccount's MasterRecordId field will contain the ID of the account it was merged into. Undeleting slaveAccount will not restore the details records back to slaveAccount. You can provide up to two slaves per master in a single merge call. You can use ID values instead of actual records, if you desire.