You need to sign in to do that
Don't have an account?
Louisa 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 !
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 !
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:
Regards,
Narender
All Answers
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:
Regards,
Narender
Thanks.