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
mikefmikef 

Merge Question

Does anyone know if you can merge a lead from the after trigger of the inserted lead?

I get the error: The master record id [SObject id] appears in the merge id list:

My thinking is you can't merge a Lead, Contact, or Account while inserting that record.

Business case:
On the insert of a lead I am querying to see if there is other leads with the same email.
If so then merge the inserted lead with the found lead.

Thank you.
ShaminaShamina

Hello,

 

I have a similar business requirement on contact object, have you managed to find a solution using merge from after trigger ?

 

Thanking you

bs881026bs881026
@ Shamina,
I have a similar requirement of merging the Contact records , while also have to assign the case associated with any duplicate record to the winning Contact record.
Please let me know if you have found a solution.
Jerun JoseJerun Jose
As per the rules of apex triggers, you cannot delete the records which are being processed. Which means that when you are inserting a record, using apex you cannot delete that record.
In a merge functionality, if the inserted record is the losing record you will need to delete this.

However, using a future method, you could achieve this.
bs881026bs881026
Hey Jerun, I dont need to delete the inserting record, I need to merge the existing record in Salesforce while assigning the Case to the Contact record which is to exist.

Thanks,
Jerun JoseJerun Jose
Merge works as updating the winning record and deleting the losing record.
bs881026bs881026
hey Jerun, I dont need to delete the inserting records, all I need to do is to merge the existing duplicate Contact records in Salesforce in a way that the Contact's Case is retained with the winning record (i.e. the record which is exist in SF after merging)
bs881026bs881026
ofcourse it works as deleting the losing record and updating the winning record, but what is the possible solution in code to do this.. any samples to help out..?

Thanks,
Jerun JoseJerun Jose
In your trigger, you will need to find out if the newly inserted contact is a duplicate or not, depending on your dup check conditions. Then you can pass the list of these contact ids to a future method.
Read more about future calls at
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

In the future method, you can now delete the losing records, so you can use the merge statement. Read more about the merge statement at
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_merge.htm