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
Eric BlaxtonEric Blaxton 

How do I typecast Lookup(user) value to a picklist

Hi and thanks in advance.

Problem:  I have a custom object that uses a Lookup (user) field.  I need this field in order to email a related user.  The problem is this field does not show up on reports.  So I created a picklist intending to populate the field from the Lookup, when either created or edited.  

How do I typecast or convert the value of a Lookup field to a picklist value.  Here is my trigger:

trigger Reg_Request_to_Reg on Registration_Requests__c (after insert) {
    List<Registration__c> newRegistration = new List <Registration__c>();
   
        for (Registration_Requests__c reg : trigger.new) {
           
            Registration__c newReg = new Registration__c();
            
            newReg.Inside_Sales_Rep__c = reg.Inside_Sales_Rep_Name__c; // Inside_Sales_Rep__c = Picklist / Inside_Sales_Rep_Name__c = Lookup
            newReg.Inside_Sales_Rep_Name__c = reg.Inside_Sales_Rep_Name__c;
            
            newRegistration.add(newReg);
           
        }//end for
        //try {
        insert newRegistration;
       //  }
       // catch (DmlException e) {
        //System.debug('The following exception has occurred: ' + e.getMessage());  
       // System.('The following exception has occurred: ' + e.getMessage());          
       // }
        }//end trigger


Best Answer chosen by Eric Blaxton
Vinit_KumarVinit_Kumar
Try below code,I think this should work :-

trigger Reg_Request_to_Reg on Registration_Requests__c (before insert) {
    List<Registration__c> newRegistration = new List <Registration__c>();
	List<Id> userIds = new List<Id>();
   
        for (Registration_Requests__c reg : trigger.new) {
			if(reg.Inside_Sales_Rep_Name__c!=null)
			{
				userIds.add(reg.Inside_Sales_Rep_Name__c);
			}
			}
           Map<Id,User> uMap = new Map<Id,User>([select id,username from User where id in:userIds]);
		   
		   for (Registration_Requests__c re : trigger.new) {
			if(re.Inside_Sales_Rep_Name__c!=null)
			{
				re.Inside_Sales_Rep__c = uMap.get(re.Inside_Sales_Rep_Name__c).username;
			}
			}
                   
        }

If this helps,please mark this as best answer to help others :)

All Answers

Eric BlaxtonEric Blaxton
I wanted to clarify the above question further.  I got the field to update by changing the data type from Picklist value to Text, but I would like to make this work by just doing a typecast if possible.  I want to avoid copying all of the data into a new field.

Thanks.
Vinit_KumarVinit_Kumar
Relationship Field are ID fields,how are you mapping it to the Picklist.Have you stored the Ids as Picklist value or what do you have in your Picklist as values ??

Eric BlaxtonEric Blaxton
I am storing names in the picklist.  The above code is not working as planned.  What I want is to cast a Lookup value to a picklist.

Is that possible?
Vinit_KumarVinit_Kumar
Try below code,I think this should work :-

trigger Reg_Request_to_Reg on Registration_Requests__c (before insert) {
    List<Registration__c> newRegistration = new List <Registration__c>();
	List<Id> userIds = new List<Id>();
   
        for (Registration_Requests__c reg : trigger.new) {
			if(reg.Inside_Sales_Rep_Name__c!=null)
			{
				userIds.add(reg.Inside_Sales_Rep_Name__c);
			}
			}
           Map<Id,User> uMap = new Map<Id,User>([select id,username from User where id in:userIds]);
		   
		   for (Registration_Requests__c re : trigger.new) {
			if(re.Inside_Sales_Rep_Name__c!=null)
			{
				re.Inside_Sales_Rep__c = uMap.get(re.Inside_Sales_Rep_Name__c).username;
			}
			}
                   
        }

If this helps,please mark this as best answer to help others :)
This was selected as the best answer
Eric BlaxtonEric Blaxton
Thanks for your answer, but I have not tried it yet.  I went ahead and changed the data type and was able to use my existing code.  I will definitely keep it in my bag of tools.

Regards