Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static void logMerge(Account[] AccountOld){
List<Merge_Audit__c> audit = new List<Merge_Audit__c>();
List<Account> mergeAccounts = new List<Account>();
Set<String> winnerIds = new Set<String>();
Map<String,String> idMap = new Map<String,String>();
Map<String,String> nameMap = new Map<String,String>();
for(Account a : AccountOld){
if(String.isNotBlank(a.MasterRecordId)){
winnerIds.add(a.MasterRecordId);
mergeAccounts.add(a);
}
}
List<Account> accs = new List<Account>([Select Id, WG_Account_ID__c, Name from Account Where Id in :winnerIds]);
for(Account a : accs){
idMap.put(a.Id, a.WG_Account_ID__c);
nameMap.put(a.Id, a.Name);
}
if(!mergeAccounts.isEmpty()){
for(Account a : AccountOld){
String jsonString = JSON.serializePretty(a);
String winnerId = idMap.get(a.MasterRecordId);
String loserId = a.WG_Account_ID__c;
Merge_Audit__c m = Utilities.createMergeAudit(a.MasterRecordId,a.Id,winnerId,loserId,'Account',jsonString);
m.Winner_Account__c = a.MasterRecordId;
m.Winning_Name__c = nameMap.get(a.MasterRecordId);
m.Losing_Name__c = a.Name;
audit.add(m);
}
}
if(!audit.isEmpty()){
insert audit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.