When the account records transferred to another user, the associated open opportunities automatically transferred to the new account owner without firing the opportunity trigger.

See my post (Account Owner changes Opportunity Owner?)

So I added code in account trigger to invoke the opportunity trigger by updating a hidden field on opportunity.

But in test class, when I change the account's ownerid, the associated opportunity ownerid is not changed by system automatically. And if I manually change the opportunity ownerid in the test class, it's like testing the opporutnity trigger.

So what's my options here? Thanks

private void updateOpptyOwnerRoleInfo()
System.debug(LoggingLevel.INFO,'Enter method UpdateOpptyOwnerRoleInfo of AccountHandler');
if (isBefore && isUpdate)
    List<Id> lstAcctIds = new List<Id>();
    for (Account acct : newList)
        Account oldAcct = oldMap.get(acct.Id);
        if (acct.OwnerId != oldAcct.OwnerId)
    if (lstAcctIds.size() > 0)
        System.debug(LoggingLevel.INFO,'Number of Account updated: '+lstAcctIds.size());
        For(Opportunity opp : [Select Id, HiddenUpdateFlip__c from Opportunity where AccountId in :lstAcctIds]){
            //OpportunityDispatcher.preventRecursion = true;

  • Can you post the code you have so far, it'll be easier for others to assist you.
    – Jenny B
    Jul 21, 2015 at 17:28
  • @JennyB I added the method in my Account trigger handler class.
    – sfdcnoob
    Jul 21, 2015 at 17:32
  • Can you add the test class as well?
    – Jenny B
    Jul 21, 2015 at 17:34

1 Answer 1


You cannot accomplish the EXACT process with Apex as you do in the UI Account Transfer Records.

In your test, all you can do is simulate the process..

  1. We know that in the UI, the Opportunity triggers do not fire

So in your test class, you will have to simply change the Account owners, then update the opportunity owners.

When you update the opportunity owners, your trigger will fire and you can assert your results.

To test the account trigger part, you will have to stop the opportunity trigger from running when you update the opps. Then you can set the flag on the Account and test that the opps were updated appropriately.

Unfortunately you have to mock up the process as there is no way to replicate exactly in Apex what happens on this process in the UI.

  • i have the same problem but i didnt understand how to mock this. the function is in AccountTrigger, not OpportunityTrigger
    – Dana Griff
    Oct 19, 2017 at 11:01

You must log in to answer this question.

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