When merging accounts, I want to be able to update the owner of the re-parented opportunities, and custom objects under account to the owner of the "winning" account.

My current trigger runs "after delete" and will grab all the associated opportunities for each account and associate them to the owner. This doesn't provide the distinction between the child records, the opportunities that were not re-parented should remain the same and not have their owners modified.

One thing that I was considering is creating a field that will be populated with a value "before delete" and access those in my "after delete" trigger. I thought this solution would be rather messy and was wondering if there was a cleaner way to figure out which records belonged to the losing account in a merger.

Thanks in advance!


trigger updateOwnerMerge on Account (after delete) {
//Create List of account IDs that have  been merged
List<Id> acctIDs = new List<Id>();
for (Account a : Trigger.Old) {
    //If the Record has been deleted through a merge, add winning account of merge to List
    if (a.MasterRecordId != NULL) {
//If there are any accounts that have been merged
if(acctIDs.size() != 0) {
    //Map to associated account ID to account owner
    Map<ID, ID> accountOwner = new Map<ID, ID>();
    //Query SOQL to find all potential accounts
    List<Account> accounts = [Select ID, OwnerID from Account where ID in: acctIds];
    for (Account acc : accounts) {
        //Add account ID, Owner association to map
        accountOwner.put(acc.Id, acc.OwnerId);        
    update updatedApps;
    //Find all opportunities related to account, update owner of open opportunities
    List<Opportunity> opps = [Select ID, OwnerID, AccountID, IsClosed from Opportunity where AccountID in: accounts];
    List<Opportunity> updatedOpps = new List<Opportunity>(); 
    for (Opportunity opp: opps) {
        if(!opp.IsClosed) {
            if(opp.OwnerID != accountOwner.get(opp.AccountID)) {
                opp.OwnerID = accountOwner.get(opp.AccountID);
    update updatedOpps;


  • Can you provide the code? Feb 12, 2014 at 4:00
  • Sure, I removed some specifics pertaining to the custom object, but essentially I do the same thing with the custom object as I do the opportunity. (Loop through and assign owners)
    – James
    Feb 12, 2014 at 4:14

2 Answers 2


You better write a trigger on Opportunity. Check if owner is same as owner of account, if not-make it same.

trigger updateOwnerMerge on Opportunity(before update) {
//query account here to fetch account owner, replace Trigger.new below with results
for(Opportunity o: Trigger.new)({
  • 1
    Thanks for the input. I'm trying to edit the opportunities under a merge process though. I've found that in the merge Account event the Opportunity triggers do not fire.
    – James
    Feb 13, 2014 at 19:55

You don't need a "field" specifically, as you can track the changes in a static variable somewhere:

trigger moveopps on account (before delete, after delete) {
        StaticVars.AffectedOpps = [SELECT Id, AccountId, OwnerId FROM Opportunity WHERE AccountId IN :Trigger.old];
    else {
        // Do your after-delete logic, since you can find the surviving record via MasterRecordId
        // StaticVars.AffectedOpps will still have your data

The class itself might be simple like:

public class StaticVars {
    public static Opportunity[] AffectedOpps;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .