function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CypherCypher 

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?

dmchengdmcheng

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

CypherCypher

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

CypherCypher

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

CypherCypher

Is it impossible to perform ANY DML inside the User object?  Is this a known limitation in SFDC?

dmchengdmcheng

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.

Starz26Starz26

Cypher wrote:

Is it impossible to perform ANY DML inside the User object?  Is this a known limitation in SFDC?


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.