I am trying to merge two lead records, please find below the code.

Lead leads = [SELECT Email,company,RFI_Record_Type__c FROM Lead WHERE email IN:emailSet and Email !=null and Id not IN:leadIds Order By CreatedDate DESC Limit 1];

List<Lead> newLeads = [SELECT Email,company,RFI_Record_Type__c FROM Lead WHERE Id IN:leadIds and Email != null];

merge leads newleads;

leads is the master record and newLeads is the duplicate/new records. Both the SOQL queries return a row, which is good. When I debug, it shows both are merged and the merged record deleted which is good.

But When I again try to assert for the merged value by using a soql over leads as same as the first SOQL above, it returns the same old record but does not display any merged value.

If we merge a merged record with master record , then will all the fields on the merged record get copied into the master record ?

Any help would be nice to have.

Thank You

  • 1
    Can you show a little more of the code that you have above these couple of lines? How are leadIds and emailSet being populated. – VictorKilo Jul 22 '13 at 16:18
  • 1
    We also need to see how you're doing the assert. – Mike Chale Jul 22 '13 at 16:31

I believe your problem is simply that you need to query back for Leads to assert on.


I found my answer .

I can have something like.


So, it updates the existing master record and deletes the merged record.

Thanks everyone for the help.

  • This is just a work around though, I feel like if this question is going benefit future users, we should dig into why this was happening... – VictorKilo Jul 22 '13 at 20:22
  • Sure. In reality if we merge a merged record with master record , then will all the fields on the merged record get copied into the master record ? – user2551 Jul 23 '13 at 13:33
  • yes, if values exist - you also don't have to worry about maintaining a field list or adding code which pulls valid fields from the schema. – VictorKilo Jul 23 '13 at 15:19

I do not have lot of knowledge on the subject of merging, but seeing as no one has answered yet, I want to throw in my two cents.

On the Merge Statement Documentation Page it states that you can either pass one or two sObjects to the merge statement as the second parameter. If you are passing two objects, you should do so in a list (as you are doing - which is good).

merge sObject sObject;
merge sObject sObject[];

What might be catching you here is that you can only merge one or two records into one record. Since I can't see what your list look like, all I can do is suggest that you ensure that at most two records are being populated into your newLeads list.

A couple of questions that you should investigate:

  1. Is newLeads always one or two records?
  2. Do you have a few similar leads in emailSet that you may be incorrectly recognizing as the same lead?
  3. Are you ensuring that your assert statement is querying fresh results for the modified lead?
  • 1
    Apex is case-insensitive - salesforce.com/us/developer/docs/apexcode/Content/… – Mike Chale Jul 22 '13 at 16:32
  • I thought that was only relevant for system variables.. my bad. – VictorKilo Jul 22 '13 at 17:50
  • Allowing case-insensitive user-defined variables seems like an excellent way to allow for sloppy/messy code. Regardless of whether or not it is allowed, I feel that programmers should attempt to be consistent with variable names. Maybe that's just my OCD. – VictorKilo Jul 22 '13 at 17:57
  • The record is getting merged, which i see in the debug log but the values are not getting updated on the master record..any pointers for this.. – user2551 Jul 22 '13 at 18:04
  • I agree that consistent naming is important. – Mike Chale Jul 22 '13 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.