You need to sign in to do that
Don't have an account?
John Archer2
Code coverage error on trigger
Hello
I have just written first trigger which works in the sandbox but am getting a code coverage error when trying to deploy
I have written a test class which I think tests one hundred per cent of the code but the application is telling me I have 0% coverage
This is despite the apex class test passing.
My trigger is:
trigger Populatecustomeventfields on Event (before update, before insert) {
For (event event :Trigger.new){
event.report_start_date__c=event.StartDatetime;
event.report_end_date__c=event.EndDateTime;
event.report_subject__c=event.Subject;
}
}
This is working correctly in the sandbox
Below is my test class and I am getting the following error: "The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage."
@isTest
public class TestPopulatecustomeventfields {
static testMethod void Insertnewevent(){
Event eventToCreate = new Event();
eventtocreate.Report_start_date__c =Datetime.valueOf ('08-08-2017 22:00:00');
eventtocreate.Report_end_date__c =Datetime.valueOf ('08-08-2017 22:00:00');
eventtocreate.Report_subject__c = 'Demo';
}
}
I have just written first trigger which works in the sandbox but am getting a code coverage error when trying to deploy
I have written a test class which I think tests one hundred per cent of the code but the application is telling me I have 0% coverage
This is despite the apex class test passing.
My trigger is:
trigger Populatecustomeventfields on Event (before update, before insert) {
For (event event :Trigger.new){
event.report_start_date__c=event.StartDatetime;
event.report_end_date__c=event.EndDateTime;
event.report_subject__c=event.Subject;
}
}
This is working correctly in the sandbox
Below is my test class and I am getting the following error: "The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage."
@isTest
public class TestPopulatecustomeventfields {
static testMethod void Insertnewevent(){
Event eventToCreate = new Event();
eventtocreate.Report_start_date__c =Datetime.valueOf ('08-08-2017 22:00:00');
eventtocreate.Report_end_date__c =Datetime.valueOf ('08-08-2017 22:00:00');
eventtocreate.Report_subject__c = 'Demo';
}
}
1) https://trailhead.salesforce.com/modules/apex_testing
Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
You write a test class for this the same way that you would any other:
- Set up some data for the Trigger to access (Event)
- Execute a method/methods:- by update Event
- Verify the behaviour with asserts.
You need to perform the update in your trigger
Let us know if this will help you
All Answers
Try below Code. If this helps you then make it solved.
Thanks
Hemant
1) https://trailhead.salesforce.com/modules/apex_testing
Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
You write a test class for this the same way that you would any other:
- Set up some data for the Trigger to access (Event)
- Execute a method/methods:- by update Event
- Verify the behaviour with asserts.
You need to perform the update in your trigger
Let us know if this will help you
Thanks for replies, have got this working now so many thanks for the replies.
The first one did not work as received an error "unknown variable "report_start_date__c" , so just intrigued to know why this would happen. I am assuming it is something to do with the list instance.
The second one did work but again was wondering why it is necessary to start with a new account - is this because an event must be attached to another object so the account is created first.
Again many thanks for replies