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
Edward Scott 5Edward Scott 5 

Apex code to copy the value of a picklist field

Hi,

I am trying to write a trigger that will update a custom field that I created on the opportunity object called case status. It is a picklist field that I want to mirror the status picklist field on cases. I also created a lookup field on opportunities called related_case. The related_case field ties the two objects together. Right now my trigger works so that after i update the field on cases I then have to press edit and save on the opportunity page to get the field to update. What I would like to happen is have the opportunity page update on refresh after changing the case field. The trigger is currently built on the opportunity and I am thinking maybe it needs to be built on the case instead. 

I am attaching the code here and any help or advice is greatly appreciated.
 
trigger updateCaseStatus on Opportunity(After Update) {

      set<id> setid =new set<id>();
    
    for(Opportunity Opp: Trigger.new){
        if(Opp.Related_Case__c != null)
        {
        setid.add(Opp.Related_Case__c);
        }
     Case cas = [select id,Status from Case where Id in: setid];
     
              for(Opportunity Opps : trigger.new)
       {
              
          Opps.Case_Status__c= cas.Status;
          opps.Related_case__c = cas.id;

             }

   }
   }




Thanks,

Edward
Best Answer chosen by Edward Scott 5
Matt SmelserMatt Smelser
Edward-
Try using the TEXT function. It will convert the picklist value into text and display the results for you.

All Answers

Matt SmelserMatt Smelser
Edward-
Why not just use a formula field to display the status field on the case?
Edward Scott 5Edward Scott 5
I tried but it said that I couldn't use a picklist value in the formula. Is there a way to do it?

Thanks
Matt SmelserMatt Smelser
Edward-
Try using the TEXT function. It will convert the picklist value into text and display the results for you.
This was selected as the best answer
Edward Scott 5Edward Scott 5
Thanks I appreciate it.