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

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.



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


Thanking you

@ 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.
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.

Jerun JoseJerun Jose
Merge works as updating the winning record and deleting the losing record.
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)
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..?

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

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