11 answers
trigger RecordOwnerChangeEx on User (after update) { list<account> lstAcc=new list<account>(); List<ID> recordIds=new List<ID> (); for(Account acc : [select id, ownerid, owner.isActive, owner.ManagerId from account where ownerId IN: trigger.new]){ if(acc.owner.isActive == false && acc.owner.ManagerId != null){ acc.ownerid = acc.owner.ManagerId; lstAcc.add(acc); recordIds.add(acc.id); } } if(!lstAcc.isEmpty()){ AccountHandler.updateAccounts(recordIds);}}public class AccountHandler { @future public static void updateAccounts(List<ID> recordIds) { List<Account> accts = [SELECT ownerid,owner.ManagerId FROM Account WHERE Id IN :recordIds]; List<Account> accts1 =new List<Account>(); for (Account acc:accts ){ acc.ownerid = acc.owner.ManagerId; accts1.add(acc); } update accts1 ; }}