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
John Archer2John 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';
    
}
}

 
 
Best Answer chosen by John Archer2
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
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
@isTest
public class TestPopulatecustomeventfields 
{
	static testMethod void Insertnewevent()
	{ 
		Account acc = new Account();
		acc.Name ='Test';
		insert acc;
		
		Event event = new Event( Subject = 'Test Event', StartDateTime=system.now(), EndDateTime=system.now()+1 , WhatId = acc.id );       
		insert event;
		
		event.Report_subject__c ='Demo1';
		update  event;
	}
}

Let us know if this will help you
 

All Answers

Hemant_SoniHemant_Soni
Hi jhon,
Try below Code.
@isTest
public class TestPopulatecustomeventfields {
    static testMethod void Insertnewevent(){ 

   list< Event> eventToCreate = new list<Event>();
    Event oEvent = 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';
    eventToCreate.add(oEvent );
    insert  eventToCreate;
    eventToCreate[0].Report_subject__c ='Demo1';
   update  eventToCreate[0];

     
}
}
If this helps you then make it solved.
Thanks
Hemant

 
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
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
@isTest
public class TestPopulatecustomeventfields 
{
	static testMethod void Insertnewevent()
	{ 
		Account acc = new Account();
		acc.Name ='Test';
		insert acc;
		
		Event event = new Event( Subject = 'Test Event', StartDateTime=system.now(), EndDateTime=system.now()+1 , WhatId = acc.id );       
		insert event;
		
		event.Report_subject__c ='Demo1';
		update  event;
	}
}

Let us know if this will help you
 
This was selected as the best answer
John Archer2John Archer2
Hello
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