You need to sign in to do that
Don't have an account?
Siva 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.
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.
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
Can you try your trigger on before update event..
Please mark as best answer if it solves your problem. Thanks
The trigger should be on the event after update. You want to update the account object from user trigger.
Thanks
Anupama
Thanks for responding. the following error popping up.
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
Thanks for your reply. The same error is popping saying setup and nonsetup objects can not be executed in the same transaction.
You need to create a seperate class for this, in which you will be using a future method to update the accounts. Like this:
Can you post the trigger code. which you are getting exception.
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;
}
Tried multiple times. This is the error with future method.
"Unsupported parameter type List<Account> at line 5 column 22"
Please try the code below
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 ;
}
}