You need to sign in to do that
Don't have an account?
Getting Role/Type of NEW Case owner after transfer of ownership
I have a trigger which is meant to update a field in a Case based on the Role or Type of the NEW owner when the case is transferred in ownership. However, it appears to be updating the field with the OLD owner's information. The end result is that the case will be transferred to the new user, but the role/type of the old user gets written to the updated field.
Clearly this means I'm doing something out of order, but I'm at the point now where I'm so frazzled I'm not going to figure it out until someone points out what I'm doing wrong. Any help is greatly appreciated.
Here's the code:
trigger getCaseOwner on Case (before update, before insert) { Case oldCase = trigger.old[0]; Case newCase = trigger.new[0]; String newOwnerId = newCase.OwnerId; String oldOwnerId = oldCase.OwnerId; String newOwnerRole = ''; String newOwnerType; newOwnerType = [select owner.type from case where id = :newCase.Id].owner.type; if ( newOwnerType == 'User' ) { newOwnerRole = [select u.UserRole.Name from User u where u.Id = :newOwnerId limit 1].UserRole.Name; } else if ( newOwnerType == 'Queue' ) { newOwnerRole = newCase.owner.name + ' Queue'; } newCase.Case_Owner_Role__c = newOwnerType; }
Looks like you've been getting yourself tied up a bit. There are a number of issues with the way that your logic is structured, chief among which is the fact that you have not made your trigger bulk safe meaning that your code will only update the first case that is in the trigger. If multiple cases are updated in the same transaction then your code will not work properly. I think the other issue that you have is that you are attempting to discover what the owner type is through a select query, but this is a before trigger so your select will return the old values.
I've not checked the following code thoroughly, but I think it should give you a better starting point than where you are right now.
Hope that helps.
Gareth
All Answers
Looks like you've been getting yourself tied up a bit. There are a number of issues with the way that your logic is structured, chief among which is the fact that you have not made your trigger bulk safe meaning that your code will only update the first case that is in the trigger. If multiple cases are updated in the same transaction then your code will not work properly. I think the other issue that you have is that you are attempting to discover what the owner type is through a select query, but this is a before trigger so your select will return the old values.
I've not checked the following code thoroughly, but I think it should give you a better starting point than where you are right now.
Hope that helps.
Gareth
In addition to my previous post I should add that you would want to substitute status with your custom field Case_Owner_Role__c
Thanks so much - I'll give it a whirl and let you know how it works out. I appreciate it.
Hi Gareth,
I, too, need to look up and store the current Case Owner's Role, and so I tried out your code. I found that it worked perfectly for existing cases, when the case owner is changed by a user. However, the trigger is not activated when new cases are created. Can you suggest any change to the code that would make the code work in this instance too?
Thanks,
Rick