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
arun Bikkasaniarun Bikkasani 

hi all can you help for writing test class for below trigger

hi i have two objects student__c  and Library__c both are related ojects


trigger createLibrary on student__c (after insert,after delete) {
    
   if(Trigger.isInsert){
       studentTriggerHandler.onStudentInsert(Trigger.New);
   }
   if(Trigger.isDelete){
       studentTriggerHandler.onStudentDelete(Trigger.Old);
   }
   
 }
handler class:
public class studentTriggerHandler{

    public static void onStudentInsert(List<student__c> studRecs){
    
        List<Library__c> lib1=new List<Library__c>(); 
        for(student__c std : studRecs){
        
            Library__c lib=new Library__c();
            lib.name=std.name;
            lib.studentname__c=std.id;
            lib.Enrollment_date__c=std.date__c;
            lib1.add(lib);
        }
        
        if(lib1.size() > 0){
            try{
                insert lib1;
            }catch(Exception ex){
                System.debug('Exeption'+ex);
            } 
          }
        }
    
    public static void onStudentDelete(List<student__c> oldStudRecs){
        
        list<id> stdlist=new list<id>();
        for(student__c  str : oldStudRecs){
            stdlist.add(str.id);
        }
        list<Library__c > liblist=[select id from Library__c where studentname__c in :stdlist];
        delete liblist;
    }

}
RHibbsRHibbs
Hi Arun,

What is the issue you are actually having difficulty with, if you could direct us to a more specific problem then we will be in a better position to assist. Post the test class you have written so far and explain the problem and we can take a look.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Arun Bikkasani, I hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar