+ Start a Discussion
Siva AdminSiva Admin 

Auto assign record owner after deactivating through trigger.

plz help me with this.
1) I am having an User (let’s say Stephan). Who has a custom Field called : Immediate Manager. (Hierarchy Field)
2) We have an Account (let’s Say : Google). Whose owner is Stephan.
3) Whenever I will deactivate that user, then that User’s Immediate manager will become owner of all Account, which Stephan owned. Code with all best practices. (Bulkifying).
Also write Test method for this , which should cover at least 85%. functionality should be checked if working or not in test Class also.

trigger RecordOwnerChangeEx on User (after update) {   
 list<user> u=[select id, isActive, ManagerId from user where id in:trigger.new];
  // list<account> acc=[select id, ownerId from Account where ownerId in: ids ];
    list<account> ac=new list<account>();
    for(User uu:u){
        if(uu.IsActive == false && uu.ManagerId != null){
        for(account a:[select id, ownerId from Account where ownerId =:uu.id ]){
            a.ownerId = uu.ManagerId;
            ac.add(a);
        }
    }
    }
    update ac;

Tried with this but not working. 
Best Answer chosen by Siva Admin
Upasna LalUpasna Lal
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 ;
    }
}

All Answers

Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi Siva,

Can you try your trigger on before update event..
 
Head In CloudHead In Cloud
Hi Siva, your trigger is not bulkified and running on a wrong event. Please try the below trigger:
trigger RecordOwnerChangeEx on User (Before update) { 
	list<account> ac=new list<account>();
	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;
			ac.add(acc);
		}
	}
	update ac;
}

Please mark as best answer if it solves your problem. Thanks
Anupama SamantroyAnupama Samantroy
Hi,

The trigger should be on the event after update. You want to update the account object from user trigger.
trigger RecordOwnerChangeEx on User (after update) { 
	list<account> lstAcc=new list<account>();
	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);
		}
	}
if(!lstAcc.isEmpty()){
	update ac;}
}

Thanks
Anupama
Siva AdminSiva Admin
Hi Anupama Samantroy
Thanks for responding. the following error popping up. 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger RecordOwnerChange caused an unexpected exception, contact your administrator: RecordOwnerChange: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012800000k4GFLAA2; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Account, original object: User: []: Trigger.RecordOwnerChange: line 18, column 1
Siva AdminSiva Admin
Hi Head In Cloud and Ankit Gupta@ Developer
Thanks for your reply. The same error is popping saying setup and nonsetup objects can not be executed in the same transaction. 
Head In CloudHead In Cloud
Hi Siva,

You need to create a seperate class for this, in which you will be using a future method to update the accounts. Like this:
trigger RecordOwnerChangeEx on User (<b>after update</b>) { 
	list<account> lstAcc=new list<account>();
	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);
		}
	}
if(!lstAcc.isEmpty()){
	AccountHandler.updateAccounts(lstAcc)}
}



public class AccountHandler {
  @future
  public static void updateAccounts(list<account> lst) {
    update lst;
  }
}

 
suresh sanneboina 4suresh sanneboina 4
Hi Siva,

Can you post the trigger code. which you are getting exception.
Siva AdminSiva Admin
trigger RecordOwnerChange on User (after update) {

    list<account> lstAcc=new list<account>();

    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);

        }
    }

if(!lstAcc.isEmpty()){

    update lstAcc;
    }
 
Siva AdminSiva Admin
Hi Head In Cloud

Tried multiple times. This is the error with future method. 
 
"Unsupported parameter type List<Account> at line 5 column 22"
suresh sanneboina 4suresh sanneboina 4
Hi,

Please try the code below
trigger RecordOwnerChange on User (after update) {

    list<account> lstAcc=new list<account>();
	Set<Id> usrId=new Set<Id>();
	for(User uid:Trigger.new)
	{
		usrId.add(uid);
	}

    for(Account acc : [select id, ownerid, owner.isActive, owner.ManagerId from account where ownerId IN: usrId]){

        if(acc.owner.isActive == false && acc.owner.ManagerId != null){

            acc.ownerid = acc.owner.ManagerId;

            lstAcc.add(acc);

        }
    }

	if(!lstAcc.isEmpty()){
		update lstAcc;
    }
}

 
Upasna LalUpasna Lal
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 ;
    }
}
This was selected as the best answer