You need to sign in to do that
Don't have an account?
M 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
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
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);
}
}
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
Please find the below trigger:
Here I considered
--> Changed the events to before.
--> Added the null checks.
--> Corrected the logic.
Please do let me know if it helps you.
Regards,
Mahesh