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
M SM S 

Trigger to update lookup field from a picklist value

Hello everyone,

I have a requirement where i have a field as Client engineer on oppty and i want to use this field as the approver in an approval process.Since, approval process wont consider the picklist value as the approver. so what i tried is to create a lookup field and get that value from the pciklist to the lookup field and hence use it for an approval. i cant update the field through configuration. Can you help me what could be the flow of trigger to accomplish this.



Thanks
MS
M SM S
This is my trigger which i have implemented 


trigger UpdatePresalesengineer on Opportunity (after Insert,after Update) {
// set to hold the string values of the picklist that are used in the affected Opportunity

   Set<String> picklistName= new Set<String>();

   // add each of the picklist values of Preventa field
   for (Opportunity opp : trigger.new) {
      picklistName.add(opp.Presales_Engineer__c);
   }

   // List of actual User records that we'll use to make our map
   List<User> u= [SELECT id, Name FROM User WHERE Name IN :picklistName];

   // Map that we'll use to access Preventa Name/id pairs
   Map<String, id> preventa= new Map<String, id>();

   for (User us : u) {
      preventa.put(us.Name, us.id);
   }

   for (Opportunity opp : Trigger.new) 
   {
      
        opp.Ingeniero_de_Preventa__c= preventa.get(opp.Presales_Engineer__c);
      
   }
}
M SM S
This is the error i am getting 


Review all error messages below to correct your data.
Apex trigger UpdatePresalesengineer caused an unexpected exception, contact your administrator: UpdatePresalesengineer: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.UpdatePresalesengineer: line 24, column 1
Mahesh DMahesh D
Hi MS,

Please find the below trigger:

Here I considered 
--> Changed the events to before.
--> Added the null checks.
--> Corrected the logic.
trigger UpdatePresalesengineer on Opportunity (before insert, before update) {

    // set to hold the string values of the picklist that are used in the affected Opportunity
    Set<String> picklistName= new Set<String>();

    // add each of the picklist values of Preventa field
    for (Opportunity opp : trigger.new) {
        if(opp.Presales_Engineer__c != null && opp.Presales_Engineer__c != '')
            picklistName.add(opp.Presales_Engineer__c);
    }

    if(!picklistName.isEmpty()) {
       
        // Map that we'll use to access Preventa Name/id pairs
        Map<String, id> preventa= new Map<String, id>();

        for (User us : [SELECT id, Name FROM User WHERE Name IN :picklistName]) {
            preventa.put(us.Name, us.id);
        }

        for (Opportunity opp : Trigger.new) {
            if(opp.Presales_Engineer__c != null && opp.Presales_Engineer__c != '')
                opp.Ingeniero_de_Preventa__c = preventa.get(opp.Presales_Engineer__c);
        }
    }
}

Please do let me know if it helps you.

Regards,
Mahesh