You need to sign in to do that
Don't have an account?

Re-assigning Cases via Apex
All-
I am trying to write some Apex code that triggers when a User is set to Inactive. When the user is set to inactive, I want to assign all cases currently owned by the user to the user's manager.
Here is the code I am using:
trigger trgCaseReassignment on User (after update) {
for(User usr:Trigger.new)
{
if (usr.IsActive == false){
String Manager = usr.ManagerId;
String uId = usr.Id;
List<Case> caserec = New List<Case>();
caserec = [SELECT c.Id, c.OwnerId from Case c where c.OwnerId =: uId FOR UPDATE];
if (caserec != null && caserec.size()>0){
for (Case c:caserec){
System.Debug('In Code');
System.Debug('*' + c.OwnerId + '*');
c.OwnerId = Manager;
System.Debug('*' + c.OwnerId + '*');
}//for (Case...)
}// if(caserec...)
}//if (usr.IsActive..)
}//for (User...)
}//class
I can tell from the logs that the correct User Id and Manager Id and Cases are found, but the case record is NOT updated. Am I unaware of some restriction that is occuring?
Also, is there a way to make the Apex code RUN AS a System Admin, and not with the restrictions of the user?
1. You need to do perform a dml update on your caserec list.
2. You should not place SOQL queries inside for loops. See this link:
http://wiki.developerforce.com/page/Apex_Code_Best_Practices
dmcheng -
Would you be willing to rewrite my code, above, to fix? I read the best practices document (thanks, by the way, our team needed this) but cannot figure out how to get the correct records returned if I can't find the user id and manage id. Finally, when I try a DML update...I get a MIXED_DML_ERROR. Any help will be appreciated.
first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Case, original object: User: []: Trigger.trgCaseReassignment: line 19, column 6
I got the Sql out of the for loop, but am still getting the MIXED_DML error on the update...
trigger trgCaseReassignment on User (after update) {
List<Case> caserec = New List<Case>();
caserec = [SELECT c.Id, c.OwnerId from Case c where c.OwnerId IN :Trigger.newMap.keySet()];
for(User usr:Trigger.new)
{
String Manager = usr.ManagerId;
if (usr.IsActive == false){
if (caserec != null && caserec.size()>0){
for (Case c:caserec){
System.Debug('In Code');
System.Debug('*' + c.OwnerId + '*');
c.OwnerId = Manager;
System.Debug('*' + c.OwnerId + '*');
}//for (Case...)
}// if(caserec...)
}//if (usr.IsActive..)
}//for (User...)
update caserec;
}//trigger
Is it impossible to perform ANY DML inside the User object? Is this a known limitation in SFDC?
Do you have some other trigger that is updating the User object? That action will fire this trigger, and I think it's all considered the same context so you get the mixed_dml error.
You cannot perform most dml updates on the user object AND on not-setup object within the same transaction
I believe you could put the non-setup objects in an @future class if you must update the user object.