+ Start a Discussion
Allen2Allen2 

Written the below piece of code but it's not working. Could anyone can correct me please?

Scenario:
When the owner of an Account is changed, check to see the Division of the new owner of the Account. If it is "Regional Sales", perform the following changes 
Fo the Account, check the Open Opportunities that are owned by the previous owner of the Account, for each Open Opportunity set the owner to be the new owner of the Account 
For the Account, check each Contact that is owned by the previous owner of the Account and set the owner to be the new owner of the Account


Apex Class:
public class AccountHandler (){
    public static void updateOwner(List<Account> newAccountList, Map<Id, Account> oldMap){
        Set<Id> accountIds = new Set<Id>();
        List<opportunity> oppList = new List<opportunity>();
        List<contact> conList = new List<contact>();
        List<Account> AccountList = new List<Account>();
        
        User usr = [Select Id, Division from User where isActive = true and Division = 'Regional Sales'];
        
        for(Account acc : newAccountList) {
            if(trigger.IsUpdate && oldMap!=null && oldMap.get(acc.Id).OwnerId != acc.OwnerId && acc.OwnerId == usr.Id){
                accountIds.add(acc.Id);
            }                               
        }
        
        if(!accountIds.IsEmpty()){
            AccountList = [Select Id, OwnerId, Owner.Division from Account where Id =: accountIds];
            for(Account acc : AccountList){
                Opportunity opp = new Opportunity();
                opp.AccountId = acc.Id;
                opp.OwnerId = acc.OwnerId; 
                oppList.add(opp);
                
                Contact con = new Contact();
                con.AccountId = acc.Id;
                con.OwnerId = acc.OwnerId;
                conList.add(con);
            } 
        }
        if(oppList.size() > 0){
            update oppList;
        }
        if(conList.size() > 0){
            update conList;
        }
    }
}

Apex Trigger:
trigger AccountTrigger on Account (after insert, after update, before delete) {
    if(Trigger.isUpdate && Trigger.isAfter){
        AccountHandler.updateOwner(Trigger.New, Trigger.OldMap);
    }
}
Hemant_SoniHemant_Soni
Hi,
Please try below one.
public class AccountHandler (){
    public static void updateOwner(List<Account> newAccountList, Map<Id, Account> oldMap){
	Set<Id> setOldUser = new Set<Id>();
	Map<Id, Account> mapAccounts = new Map<Id, Account>();
        List<sObject> lstsObject = new List<sObject>();
        
        for(Account acc : [SELECT Id, Name, OwnerId, Owner.Division, Owner.isActive FROM Account Where Id IN : newAccountList]) {
            if(oldMap != null && oldMap.get(acc.Id).OwnerId != acc.OwnerId){
				if(acc.Owner.Division == 'Regional Sales' && acc.Owner.isActive){
					setOldUser.add(oldMap.get(acc.Id).OwnerId);
					mapAccounts.put(acc.Id, acc);
				}
            }                               
        }
		
		if(setOldUser.size() > 0){
			for(Opportunity opp : [SELECT Id, Name, AccountId, OwnerId FROM Opportunity Where AccountId IN : mapAccounts.keyset() AND StageName = 'Open' AND OwnerId IN : setOldUser]){
				if(mapAccounts.containsKey(opp.AccountId)){
					opp.OwnerId = mapAccounts.get(opp.AccountId).OwnerId;
					lstsObject.add(opp);
				}
			}
			for(Contact oContact : [Select Id, Name, AccountId, OwnerId From Contact Where AccountId IN : mapAccounts.keyset() AND OwnerId IN : setOldUser]){
			if(mapAccounts.containsKey(oContact.AccountId)){
					opp.OwnerId = mapAccounts.get(oContact.AccountId).OwnerId;
					lstsObject.add(oContact);
				}
			}
			if(lstsObject.size() > 0){
				update lstsObject;
			}
		}
    }
}

If need any other help or assistance you can directly contact me on my email id.
 

Thanks
Hemant
Email : sonihemant.jaipur@gmai.com

Vikash GoyalVikash Goyal
Hi Allen,

You can try this :
public class AccountHandler (){
    public static void updateOwner(List<Account> newAccountList, Map<Id, Account> oldMap){
        Set<Id> accountIds = new Set<Id>();
        List<opportunity> oppList = new List<opportunity>();
        List<contact> conList = new List<contact>();
        List<Account> accountList = new List<Account>();
        
        for(Account acc : newAccountList) {
            if(oldMap != null && oldMap.get(acc.Id).OwnerId != acc.OwnerId){
                accountIds.add(acc.Id);
            }                               
        }
        
        if(!accountIds.IsEmpty()){
            accountList = [SELECT Id, OwnerId, Owner.Division, (SELECT Id FROM Contacts), (SELECT Id FROM Opportunities WHERE IsClosed = false) FROM Account WHERE Id IN :accountIds AND Owner.Division = 'Regional Sales'];
            for(Account acc : accountList){
                if(!acc.Opportunities.IsEmpty()){
                    for(Opportunity opp : acc.Opportunities){
                        opp.OwnerId = acc.OwnerId; 
                        oppList.add(opp);
                    }
                }
                if(!acc.Contacts.IsEmpty()){
                    for(Contact con : acc.Contacts){
                        con.OwnerId = acc.OwnerId;
                        conList.add(con);
                    }
                }
            } 
        }
        if(oppList.size() > 0){
            update oppList;
        }
        if(conList.size() > 0){
            update conList;
        }
    }
}

Please mark this as answer if it works for you.

Thanks​​​​​​​