+ Start a Discussion
NandhuNandhu 

i want to write to test class and also check my trigger program

trigger LeavesTrigger on Leaves__c (after update, after insert) {
  if (trigger.isAfter && trigger.isUpdate) {
    TriggerHandlerLeave.onAfterUpdate(Trigger.new,Trigger.oldMap);
   }    
}
public with sharing class TriggerHandlerLeave {
  
     public static void onAfterUpdate(List<Leaves__c> leaveList){
        Map<Id, Leaves__c> merMap = new Map<Id, Leaves__c>([Select Id, Name,Approval_Status__c,
                                                     Total_Leave__c,
                                                     Req_Days_Off__c 
                                                     From Leaves__c]);         
        
         for(Leaves__c approvedList:leaveList){
            if(approvedList.Approval_Status__c =='Approved'){
              Leaves__c   objLeave= new Leaves__c();
               merMap.get(approvedList.Id).Total_Leave__c = merMap.get(approvedList.Id).Total_Leave__c - merMap.get(approvedList.Id).Req_Days_Off__c;
               leaveList.add(objLeave);
                }
           }      
                Insert leaveList;
     }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Nandu,

You can change the soql query to Select Id, Name,Approval_Status__c, Total_Leave__c, Req_Days_Off__c From Leaves__c Where Approval_Status__c ='Approved' to get only those which have status as approved I hope this helps and for writing a test class for trigger you can insert a new record with the status as approved and making changes to the leaves so that the code coverage is reached and just for information the code coverage for the apex trigger can be 1 % but for apex classes they should have 75%.

In case if it was useful can you please choose this as the best answer.

Regards,
Anutej