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
Cody Drennon 7Cody Drennon 7 

How to retrieve the value of a picklist that was selected by a user, using a trigger

Below i am trying to replace User.Name with the name of my picklist field(Change_Owner__c) and then Cody Dren with whatever the user selected from the picklist(Cody D, Kyle W, Jen J etc).  Can someone please help me with this. 

Thanks.

Trigger UpdateOwnerID on Case (before insert, before update) {
       
    User objUser = [select Id from User where User.Name = 'Cody Drennon' limit 1];  
    for (Case objCase : Trigger.new)
    {
        if (objCase.OwnerId <> objUser.Id)               
        {
            objCase.OwnerId = objUser.Id;
        }
    }

}
Best Answer chosen by Cody Drennon 7
Paul S.Paul S.
Got It. 

What you're trying to do is take a field value from your in scope Case (objCase) and find a User.  To do that, you have to have something on the user record that will match the value from the Change_Owner__c field.  If you were to add a field on the user record called Case_Owner_Code__c (again, it doesn't matter what you call it), something like this should work:
Trigger UpdateOwnerID on Case (before insert, before update) {
       
    Map<String, Id> mapOfUserIdByCaseOwnerCode = new Map<String, Id>();
    
    for(User u : [SELECT Name, Case_Owner_Code__c FROM User WHERE Case_Owner_Code__c != null]){
        mapOfUserIdByCaseOwnerCode.put(u.Case_Owner_Code__c, u.Id);
    }

    for (Case objCase : Trigger.new)
    {
        Id targetOwnerUserId = mapOfUserIdByCaseOwnerCode.get(objCase.Change_Owner__c);

        if (targetOwnerUserId != null && objCase.OwnerId != targetOwnerUserId)               
        {
            objCase.OwnerId = targetOwnerUserId;
        }
    }
}

 

All Answers

Paul S.Paul S.
Cody - I'd try adding a field on your user object, Change_Owner_Code__c (what you call it is irrelevant), and then make the value in that field on the user object equal to whatever the picklist choice might be.
Cody Drennon 7Cody Drennon 7
Thank you for the response.  When i change "Cody Drennon" to the case.Hidden_Owner_Change__c.  I get the following error "Error: Compile Error: Unexpected token 'objUser'. at line 4 column 10"

Trigger UpdateOwnerID on Case (before insert, before update) {
       
       
    User objUser = [select Id from User where User.Name = Case.Hidden_Owner_Change__c limit 1];  
    
    for (Case objCase : Trigger.new)
    {
        if (objCase.OwnerId <> objUser.Id)               
        {
            objCase.OwnerId = objUser.Id;
        }
    }

}
Paul S.Paul S.
That query isn't going to work because there's no concept of the variable "Case" at that point.  For clarification, where does the field Change_Owner__c live?  Case or User?
Cody Drennon 7Cody Drennon 7
Lives in case.  It is populated from a picklist in case.
Paul S.Paul S.
Got It. 

What you're trying to do is take a field value from your in scope Case (objCase) and find a User.  To do that, you have to have something on the user record that will match the value from the Change_Owner__c field.  If you were to add a field on the user record called Case_Owner_Code__c (again, it doesn't matter what you call it), something like this should work:
Trigger UpdateOwnerID on Case (before insert, before update) {
       
    Map<String, Id> mapOfUserIdByCaseOwnerCode = new Map<String, Id>();
    
    for(User u : [SELECT Name, Case_Owner_Code__c FROM User WHERE Case_Owner_Code__c != null]){
        mapOfUserIdByCaseOwnerCode.put(u.Case_Owner_Code__c, u.Id);
    }

    for (Case objCase : Trigger.new)
    {
        Id targetOwnerUserId = mapOfUserIdByCaseOwnerCode.get(objCase.Change_Owner__c);

        if (targetOwnerUserId != null && objCase.OwnerId != targetOwnerUserId)               
        {
            objCase.OwnerId = targetOwnerUserId;
        }
    }
}

 
This was selected as the best answer
Paul S.Paul S.
One other note: Case_Owner_Code__c (on the User object) must be set to be unique and the value in that field must match whatever's in Change_Owner__c field on the Case object.
Cody Drennon 7Cody Drennon 7
Thanks Paul