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 

i need to write the test class and check my trigger is correct or wrong?

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();
   
   if(trigger.isAfter || trigger.isUpdate){
      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.oldMap){
 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 Nandu,

I think there is another question with the same details I guess I was able to answer your query over there.

Regards,
Anutej