Skip to content

Instantly share code, notes, and snippets.

@mpassovoy
Created January 15, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpassovoy/79831862febe3d5d0bda34674e481eb7 to your computer and use it in GitHub Desktop.
Save mpassovoy/79831862febe3d5d0bda34674e481eb7 to your computer and use it in GitHub Desktop.
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