You need to sign in to do that
Don't have an account?

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;
}
}
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;
}
}
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.
- please check for Test Class Generator App from APP Exchange.Please refer the below link.
- https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD
- Please follow below Salesforce Best Practice Link on Test Classes:-
- http://amitsalesforce.blogspot.sg/2015/06/best-practice-for-test-classes-sample.html
I hope it will be helpful.Please mark it as best answer if the information is informative.
Best Regards
Rahul Kumar