• scholzie
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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

 

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