+ Start a Discussion
scholziescholzie 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
DevGarethmdDevGarethmd

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.

 

trigger getCaseOwner on Case (after update) {
    
    set<id> userIds = new set<id>();
    set<id> caseIds = new set<id>();
    
    
     for(integer x=0; x<trigger.newMap.size();x++){
        if(trigger.old[x].ownerId != trigger.new[x].ownerId){
            caseIds.add(trigger.new[x].id);
            
        
        }
    }
    
    
    map<id, case> caseMap = new map<id, case>([select id, owner.type, status from case where id in: caseIds]);
    
    
    for(integer x=0; x<trigger.newMap.size();x++){
        if(trigger.old[x].ownerId != trigger.new[x].ownerId){
           if(caseMap.get(trigger.new[x].id).owner.type == 'User'){
                userIds.add(trigger.new[x].ownerId);
            }
            
        
        }
    }
    
    map<id, User> userMap = new map<id, User>([select u.id, u.UserRole.Name from User u where u.Id in :userIds]);
    
    list<Case> casesToUpdate = new list<Case>();
    
    for(integer x=0; x<trigger.newMap.size();x++){
        if(trigger.old[x].ownerId != trigger.new[x].ownerId){
           if(caseMap.get(trigger.new[x].id).owner.type == 'User'){
                caseMap.get(trigger.new[x].id).status = userMap.get(trigger.new[x].ownerId).UserRole.Name;
            }
            else{
                caseMap.get(trigger.new[x].id).status = 'Queue';
            }
         
        }
    }

    update caseMap.values();

}

 Hope that helps.

Gareth

All Answers

DevGarethmdDevGarethmd

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.

 

trigger getCaseOwner on Case (after update) {
    
    set<id> userIds = new set<id>();
    set<id> caseIds = new set<id>();
    
    
     for(integer x=0; x<trigger.newMap.size();x++){
        if(trigger.old[x].ownerId != trigger.new[x].ownerId){
            caseIds.add(trigger.new[x].id);
            
        
        }
    }
    
    
    map<id, case> caseMap = new map<id, case>([select id, owner.type, status from case where id in: caseIds]);
    
    
    for(integer x=0; x<trigger.newMap.size();x++){
        if(trigger.old[x].ownerId != trigger.new[x].ownerId){
           if(caseMap.get(trigger.new[x].id).owner.type == 'User'){
                userIds.add(trigger.new[x].ownerId);
            }
            
        
        }
    }
    
    map<id, User> userMap = new map<id, User>([select u.id, u.UserRole.Name from User u where u.Id in :userIds]);
    
    list<Case> casesToUpdate = new list<Case>();
    
    for(integer x=0; x<trigger.newMap.size();x++){
        if(trigger.old[x].ownerId != trigger.new[x].ownerId){
           if(caseMap.get(trigger.new[x].id).owner.type == 'User'){
                caseMap.get(trigger.new[x].id).status = userMap.get(trigger.new[x].ownerId).UserRole.Name;
            }
            else{
                caseMap.get(trigger.new[x].id).status = 'Queue';
            }
         
        }
    }

    update caseMap.values();

}

 Hope that helps.

Gareth

This was selected as the best answer
DevGarethmdDevGarethmd

In addition to my previous post I should add that you would want to substitute status with your custom field Case_Owner_Role__c 

scholziescholzie

Thanks so much - I'll give it a whirl and let you know how it works out. I appreciate it.

RickSRickS

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