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
chanti kchanti k 

How to update fields when status is approved using trigger

Hi All,

I have 3 fileds on opportunity. these are fields
1)status is picklist, values of status : Approved, Rejected and In progress
2)App_date 
3)App year.

But Here my reuirements is when ever status is Approved then i want update App_date  and App_year. .but  App year is only current year  .

I did not get year format "YYYY".. 

please see the below eample for year .

Note :App_year = 2018.

Here my trigger.
trigger approvalupadte on Opportunity (before update, before insert)
{
  for(Opportunity opp : Trigger.new){
      if(opp.Status__c=='Approved')
      {
      
      opp.App_year ='YYYY';
      opp.App_date =system.today();
      }
      
     
        
  }

}
Kindly help me for Current year format, how to i will get
Thanks,
Chanti.
Kai Herng LauKai Herng Lau
Hi Chanti,

Can you try using System.today().year();?

The return of year() is integer, if your App_year is String please convert using the following method
String.valueOf(System.today().year());
Hope this help.