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

Is it possible to use Merge method outside of a For Loop?...Bulkify Merge statement?

I have the following requirement:


Our contact records are synced with another database via informatica. We need to be able to reflect merge events that happen in the other database in our SFDC org.  So, we have a  field on Contact object called MergeMasterID__c. This field is meant to hold the SFID of a contact record, which would be the master contact in a merge process. The idea is that when a merge takes place in the other database, informatica can push the SFID of the master record into the MergeMasterID__c field of the child record. 


This allows me to write a trigger that is pulled when the MergemasterID__c field on a Contact is updated. The trigger then finds the the master contact record and merges the two together.


Anyway, what I want to figure out with this post is how to execute a merge statement outside of a for loop. the docs only demonstrate how to do them one at a time


Here is the for loop where I currently do the merge. I'm obviously going to hit governor limits at some point with this approach. So, I need to bulikify this somehow?


//iterate through master records and merge children
	for(Contact masterCon1 : masterCons.values()) {
		merge masterCon1 masterChildMap.get(masterCon1.Id);





You Can't do. this si not possible to use Merge method outside of a For Loop.


if this is correct then tick the answers.


using a merge statement never dependes on looping structure or not. But only dependency is it should merge up to 3 records  of the same  sObject type into one of the records, deleting the others, and re-parenting any related records.

in the below example i am using the merge statement normally

List<Account> ls = new List<Account>{new Account(name='Acme Inc.'),new Account(name='Acme')};
insert ls;
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
try {
merge masterAcct mergeAcct;
} catch (DmlException e) {
// Process exception here