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
Louisa Pidcock 11Louisa Pidcock 11 

Please help me to write test class for simple after insert Trigger :)

Hi Guys 

I have just started to try and learn how to code and have sucessfully managed  my get the trigger to work  as expected.   However I am really struggling with  the test class.. I just cant seem to get my head around it. 
Logic - We have a custom object called Buyer_Contact__c. on which there is a checkbox field called 'Inacte KPI' which is populate dunder certain conditions. I would like to delete any records that have this box cheks after update.  

Below is the working  trigger :

trigger InactiveKPIdelete on Buyer_Contact__c (after update) {

 List<Id> lstId = new List<Id>();    
    
    for(Buyer_Contact__c buyerCon: Trigger.old){
        
        List<Buyer_Contact__c> buyerConList = [SELECT Id FROM Buyer_Contact__c WHERE Inactive_KPI__c = TRUE] ;
        
        delete buyerConList;
    }
}


Here is my attempt at a test class (try not to laugh) 

@isTest 
private class InactivekpiTest {
    static testMethod void InactiveKPIdelete() {
      Buyer_Contact__c bcon = new Buyer_Contact__c (Inactive_kpi__c = true) ;
 
 
       // Insert buyer contact
       insert bcon;
 
   
    }
}



Can anyone guide me please as my trigger is at 0% coverage lol 

Many thanks in advance for any help you can give me ! 
Best Answer chosen by Louisa Pidcock 11
Narender Singh(Nads)Narender Singh(Nads)
Hi Louisa,

The reason you are getting 0% coverage is because your trigger is not being called. And the reason for that is your trigger is working on after update event(It means that it will execute when the record gets updated).
In your test class, you are just inserting the record, and not updating it.

In your test class first you will have to create a record and then update it in order to fire the trigger.

Test class will look something like this:
@isTest 
private class InactivekpiTest {
    static testMethod void InactiveKPIdelete() {
      Buyer_Contact__c bcon = new Buyer_Contact__c (Inactive_kpi__c = false) ;
       
       // Insert buyer contact
       insert bcon;

      bcon.Inactive_kpi__c =true;
      update bcon;
 
   
    }
}

Regards,
Narender​

All Answers

Raj VakatiRaj Vakati
Use this code
 
@isTest 
private class InactivekpiTest {
    static testMethod void InactiveKPIdelete() {
      Buyer_Contact__c bcon = new Buyer_Contact__c (Inactive_kpi__c = true) ;
 
 
       // Insert buyer contact
       insert bcon;
	   
	   bcon.Inactive_kpi__c = false ;
	   update bcon ;  
	   
 
 
 
  Buyer_Contact__c bco1 = new Buyer_Contact__c (Inactive_kpi__c = false) ;
 
 
       // Insert buyer contact
       insert bco1;
	   
	   bco1.Inactive_kpi__c = true ;
	   update bco1 
 
 
   
    }
}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi Louisa,

The reason you are getting 0% coverage is because your trigger is not being called. And the reason for that is your trigger is working on after update event(It means that it will execute when the record gets updated).
In your test class, you are just inserting the record, and not updating it.

In your test class first you will have to create a record and then update it in order to fire the trigger.

Test class will look something like this:
@isTest 
private class InactivekpiTest {
    static testMethod void InactiveKPIdelete() {
      Buyer_Contact__c bcon = new Buyer_Contact__c (Inactive_kpi__c = false) ;
       
       // Insert buyer contact
       insert bcon;

      bcon.Inactive_kpi__c =true;
      update bcon;
 
   
    }
}

Regards,
Narender​
This was selected as the best answer
Louisa Pidcock 11Louisa Pidcock 11
 Guys!! thank you so much!!! This is exactly the advise I needed to understand ! I really appreciate it very much -  thanks again I will try it out now 
Narender Singh(Nads)Narender Singh(Nads)
Please mark a best answer if it helps you solve your problem, so that others with similar issue can benefit from this post.

Thanks.