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
NandhuNandhu 

my program is correct or not for picklist filed

Scenario:Trigger to be written when a new leave request created is approved.
When the Approval process incurs and leave is approved, the Leave status field is changed to Approved.

Criteria : Trigger should work when the 'Approval status' field on Leaves object changes its picklist value to 'APPROVED'
Action : Total Available Leaves = Total Available leaves - Requested Days Off
Trigger:
trigger LeavesTrigger on Leaves__c (after update) {
  TriggerHandlerLeave handler = new TriggerHandlerLeave();  
   handler.onAfterInsert();
}

Handler Class
public with sharing class TriggerHandlerLeave {
 public void onAfterInsert(){
    totalLeave();
  }
Map<Id, Leaves__c> merMap = new Map<Id, Leaves__c>([Select Id, Name,Approval_Status__c,
                                                     Total_Available_Leave__c,
                                                     Req_Days_Off__c 
                                                     From Leaves__c]);
public void totalLeave(){ 
 for(Leaves__c approvedList:Trigger.New){
 if(approvedList.Approval_Status__c =='Approved'){               
 merMap.get(approvedList.Leaves__c).Total_Available_Leave__c=merMap.get(approvedList.Leaves__c).Total_Available_Leave__c - merMap.get(approvedList.Leaves__c).Req_Days_Off__c;
 
       }
}      
update merMap.values();
}
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Nandini,

You can try using the conext variables so that in case if you want to extend your trigger in the future and follow the best practice of one trigger on one object.The trigger context variables can be trigger.isafter and trigger.isupdate this could be a condtion check then you can call your function

In the soql query I believe you can create a map in the begining then store the response of records in a list and then iterate through the list of records to store the values in the map.

Also instead of checking the records that are approved late you can write a filter in the soql query such that only those records are returned which satisfy the condition.

Then you can make changes to the record values or perform the operation to the total leaves so as to save them or update them later and also do keep in mind to follow the best practices when you are creating a trigger.

In case if the above was helpful in your query can you please mark this as the best answer as it would help others in the future and also helps in keeping our community clean.

Regards,
Anutej
boliset kumarboliset kumar
as it is an update try to compare the status from the trigger.new and sttaus from trigger.oldmap so that you are sure that your class will be executed only for this update