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
Miki NagikawaMiki Nagikawa 

Update a picklist value to a picklist value using APEX

I have a list of values in one picklist field (A) and would like to have some of those values (abc, dfe) trigger to update another picklist field's (B) value (xyz) upon selection. The reason i would like to use Apex is because I have a field dependency already used for those fields and cannot overwrite it with another dependency. Any suggestion? I am new to apex and I have trouble creating the trigger.
Best Answer chosen by Miki Nagikawa
ajay rawat 14ajay rawat 14
Hi Miki
      You can use picklist field the same way as a plain text field.
if(obj.picklistValueA == 'abc' || obj.picklistValueA == 'dfe')
{
   obj.picklistValueB ='xyz';
}

Thanks and Regards
Ajay Rawat

All Answers

ajay rawat 14ajay rawat 14
Hi Miki
      You can use picklist field the same way as a plain text field.
if(obj.picklistValueA == 'abc' || obj.picklistValueA == 'dfe')
{
   obj.picklistValueB ='xyz';
}

Thanks and Regards
Ajay Rawat
This was selected as the best answer
Miki NagikawaMiki Nagikawa
Hi Ajay

Thank you for your reply!
I am getting an error as shown below:
Apex Trigger Opp error
Do you know where it is wrong?
Miki
Miki NagikawaMiki Nagikawa
I am sorry, I removed the '{'s and got it to work! thank you!
Miki NagikawaMiki Nagikawa
One more question...
If I would like this trigger to run BEFORE saving, what or where would I modify?

Miki
ajay rawat 14ajay rawat 14
Yeah for this you have to add before update in first line
(after insert, after update , before insert, before update)
Miki NagikawaMiki Nagikawa
Thank you.. I added that to the first line but it is not working. It seems like the trigger is fired when saving. So it shows the update after it is saved.
Is there a way it would update while editing/creating new opportunity?
Rajiv MishraRajiv Mishra
Just you need to add the before update statement as ajay told and it should work
shyam sundar 112shyam sundar 112

Can you help me just opposite to this.

Lookup to picklist value updation using trigger