function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Danielle SDanielle S 

Prevent Opportunities transfer when the account is transfered

Hi everyone, 
When an account is transfered , related opportunities are also been transferred. I need to prevent that. 
I used Triggers and Apex Class that I've copied from a forum. I am not very familiar with coding.
Indeed, this solution is only working when the account is newly created. It doesn't work for the existing accounts and opportunities.
Also, it's not working if there is an active automation process. 

Could you please help to resolve the problem ? Thank you in advance for your help! 
Please, see below all the elements I've used.
2 custom fields have been created : previous owner and Owner Flag
 
Account Trigger : 
trigger AccountTrigger on Account (before insert,after insert,after update,before Update)
{
           if(Trigger.IsUpdate && Trigger.isAfter)
           {
               AccountUtilities.updateOppOwnerOnOpp(trigger.new,trigger.newMap,trigger.oldmap);
           }  
}

Opportunity Trigger 
trigger OpportunityTriggers on Opportunity (before insert,after insert,before update,after update) 
{
    if(Trigger.isInsert && trigger.isBefore){
        OpportunityUtils.updatePreviousOwner(trigger.new);
    }
     if(Trigger.isUpdate && trigger.isBefore){
        OpportunityUtils.updatePreviousOwnerBeforeUpdate(trigger.new,trigger.oldMap);
    }
    if(trigger.isAfter && Trigger.isUpdate){
        OpportunityUtils.assignPrevOwner(trigger.new,trigger.oldMap);
    }
}

Account Apex class : 

public class AccountUtilities
{
   
    public static void updateOppOwnerOnOpp(List<Account> lstAccs,Map<Id,Account> newMap,Map<Id,Account> oldMap){
        set<id> accId = new set<id>();
        List<opportunity> oppList = new List<Opportunity>();
        List<opportunity> oppListToUpdate = new List<Opportunity>();
        for(Account acc:lstAccs)  {
           if(newMap.get(acc.id).ownerId!=oldMap.get(acc.id).ownerId){
             accId.add(acc.Id);   
            }
        }
oppList = [select id,Owner_Flag__c from opportunity where accountid in :accId AND StageName!='Closed Won' AND StageName!='Closed Lost'] ;
               for(opportunity opp:oppList){
            if(opp.Owner_Flag__c==true){
              opp.Owner_Flag__c=false;  
            }else{
               opp.Owner_Flag__c=true;   
            }            
               oppListToUpdate.add(opp);
            }
   
        if(oppListToUpdate.Size()>0){
            update oppListToUpdate;
        }
    }
}

Opportunity Apex Class : 
public class OpportunityUtils 
{
    public static void updatePreviousOwner(List<Opportunity> oppList){
        for(Opportunity objOppty :oppList){                
            objOppty.Previous_Owner__c=objOppty.OwnerId;
        }
    }
    
        public static void updatePreviousOwnerBeforeUpdate(List<Opportunity> oppList,Map<Id,Opportunity> oldMap){
            for(Opportunity objOppty :oppList){
                if(trigger.isUpdate){
                    if(objOppty.ownerId!=oldMap.get(objOppty.id).ownerId){
                        objOppty.Previous_Owner__c=objOppty.OwnerId; 
                    }
                }
        }
    }
    public static void assignPrevOwner(List<Opportunity> oppList,Map<Id,Opportunity> oldMap){
       List<Opportunity> oppToUpdateList = new List<Opportunity>(); 
       for(Opportunity objOppty :oppList){
                    if(objOppty.Owner_Flag__c!=oldMap.get(objOppty.id).Owner_Flag__c){
                        opportunity op = new opportunity(id=objOppty.id);
                        if(objOppty.Previous_Owner__c!=null){
                            op.ownerId=objOppty.Previous_Owner__c; 
                            oppToUpdateList.add(op);
                        }
                    }
       }
       if(oppToUpdateList.Size()>0){
            update oppToUpdateList;
        } 
    }
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Daya,

Can you try using the flow in the update scenerio as shown below and confirm if this works for you.

https://jorydean.medium.com/convert-opportunity-owner-during-account-owner-change-salesforce-953b1035bbe1

If this solution helps, Please mark it as best answer.

Thanks,
 
Danielle SDanielle S
Hi Sai, 
thank you for your answer, but the flow is not working everytime ! 
for instance, I have an account owned by Bryan with an opportunity also owned by bryan. i I transfer the opportunity to Tom , the opportunity owner doesn't change . But if I transfer the account back to Bryan, the opportunity is transfered to Tom. Odd