Before we go ahead we should know about standard functionality of Account Merge Click Here.
Account Merge behavior for Account team member records : If both accounts have different account teams, the merged account will contain members from both account teams.
Here standard behavior will put all team members of both account under the merged account.What if you don’t want to bring account team members of the other account ?
So basically when we are merging two accounts one gets deleted and another gets updated. We can write a trigger on the Delete event of account, whenever an account is going to be deleted we will also delete its team members. The code snippet for stopping copying of account team members of losing account to the winning account is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 |
if(Trigger.isBefore && Trigger.isDelete){ // Collecting all Team members to delete List accTeamMemberToDelete = new List(); for(account acc : [SELECT id,(SELECT id FROM AccountTeamMembers) FROM account WHERE id IN :Trigger.old]){ if(acc.AccountTeamMembers != null){ accTeamMemberToDelete.addAll(acc.AccountTeamMembers); } } if(accTeamMemberToDelete.size() > 0){ delete accTeamMemberToDelete; } } |