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
KatherineCKatherineC 

Test Class Runs Successful But No Code Coverage

Hi All,
I created a trigger so that when a new event is created in Public Calendar a new record under custom object CalendarEV will be created automatically, I also created the test class, it runs successfully but no code coverage, please help.

trigger CalendarEV on Event (after insert) {
    List<Event> Events = new List<Event>();
   for (Event e : Trigger.new) {
           if (e.Subject != null ) {
               CalendarEV__c C = new CalendarEV__c();
               c.Name = e.Subject;
               c.Start__c = e.StartDateTime;
               c.End__c = e.EndDateTime;
               c.Location__c = e.Location;
               c.Description__c = e.Description;
                
              insert C;
        }
    }
   }


@isTest
public class CalendarEVTest
{
    static testMethod void insertNewCalendarEV()
    {
        Test.StartTest();
        Event event = new Event();
        event.Subject = 'Test Event';
        event.Location = 'Test Location';
        event.Description = 'Test Description';      
        Test.StopTest();
    }
}
Best Answer chosen by KatherineC
Ramakrishnan AyyanarRamakrishnan Ayyanar
you tried this code.

@isTest
public class TestCalendarEV
{
    static testMethod void insertNewCalendarEvent()
    {
        Test.StartTest();
        Event testEvent= new Event();
        testEvent.Subject = 'EventTest';
        testEvent.Location = 'Location';
        testEvent.Description = 'Description'; 
        testEvent.IsAllDayEvent=true;
        testEvent.StartDateTime = datetime.newInstance(2014, 11, 13, 16, 30, 0);
        testEvent.EndDateTime = datetime.newInstance(2014, 11, 13, 18, 30, 0);
        insert testEvent;
        Test.StopTest();
        System.assertEquals ('EventTest', testEvent.Subject);
    }
}

All Answers

James LoghryJames Loghry
You never insert the event, so it never fires the trigger.

Add an "insert event" before your Test.stopTest();

You also need to requery the Event record, and use System.assert calls to verify your trigger is working as expected.

Read up on apex tests with the following link: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
KatherineCKatherineC
Hi James,
Thanks for the reply, I changed it but still no code coverage, please help:

Error message: System.DmLException: insert failed. First exception on row 0; First error, Required field missing
Stack Trace: Class.CalendarEVTest.insertNewCalendarEV: Line11, colum 1

@isTest
public class CalendarEVTest
{
    static testMethod void insertNewCalendarEV()
    {
        Test.StartTest();
        Event e = new Event();
        e.Subject = 'Test Event';
        e.Location = 'Test Location';
        e.Description = 'Test Description';  
        insert e;
        Test.StopTest();
        System.assertEquals ('Test Event', e.Subject);
    }
}
KatherineCKatherineC
It turns out both Start and End fields are required fields for Event, by adding these 2 fields in test class then it works. Here's the final code:

@isTest
public class CalendarEVTest
{
    static testMethod void insertNewCalendarEV()
    {
        Test.StartTest();
        Event e = new Event();
        e.Subject = 'Test Event';
        e.Location = 'Test Location';
        e.Description = 'Test Description';  
        e.StartDateTime = datetime.newInstance(2014, 9, 15, 12, 30, 0);
        e.EndDateTime = datetime.newInstance(2014, 9, 15, 13, 30, 0);

        insert e;
        Test.StopTest();
        System.assertEquals ('Test Event', e.Subject);
    }
}
kevin lamkevin lam
You haven't specified IsAllDayEvent in your test event, therefore you'll need to specify StartDateTime and DurationInMinutes/EndDateTime.
Ramakrishnan AyyanarRamakrishnan Ayyanar
you tried this code.

@isTest
public class TestCalendarEV
{
    static testMethod void insertNewCalendarEvent()
    {
        Test.StartTest();
        Event testEvent= new Event();
        testEvent.Subject = 'EventTest';
        testEvent.Location = 'Location';
        testEvent.Description = 'Description'; 
        testEvent.IsAllDayEvent=true;
        testEvent.StartDateTime = datetime.newInstance(2014, 11, 13, 16, 30, 0);
        testEvent.EndDateTime = datetime.newInstance(2014, 11, 13, 18, 30, 0);
        insert testEvent;
        Test.StopTest();
        System.assertEquals ('EventTest', testEvent.Subject);
    }
}
This was selected as the best answer
KatherineCKatherineC
Hi Ramakrishnan,
It works, thanks so much!! Now I have another problem as I only want trigger fires when new event created in Public Calendar, I found out my trigger will fire when user create events in their own calendar too. I changed the If in the code but not working, any suggestion?

1st Try:

Error: Compile Error: Illegal assignment from String to SOBJECT:Name at line 4 column 1

trigger CalendarEV on Event (after insert) {
List<Event> Events = new List<Event>();
   for (Event e : Trigger.new) {
           if (e.Owner = 'AdvisorsLink_Calendar' ) {
               CalendarEV__c C = new CalendarEV__c();
               c.Name = e.Subject;
               c.Start__c = e.StartDateTime;
               c.End__c = e.EndDateTime;
               c.Location__c = e.Location;
               c.Description__c = e.Description;
                     
              insert C;
        }
    }
   }

2nd Try:

Error: Compile Error: Comparison arguments must be compatible types: SOBJECT:Name, SOBJECT:User at line 4 column 16

trigger CalendarEV on Event (after insert) {
List<Event> Events = new List<Event>();
   for (Event e : Trigger.new) {
           if (e.Owner != e.CreatedBy ) {
               CalendarEV__c C = new CalendarEV__c();
               c.Name = e.Subject;
               c.Start__c = e.StartDateTime;
               c.End__c = e.EndDateTime;
               c.Location__c = e.Location;
               c.Description__c = e.Description;
                     
              insert C;
        }
    }
   }


Ramakrishnan AyyanarRamakrishnan Ayyanar
Hi KatherineC,


Try This Code.


trigger CalendarEV on Event (after insert)
{
   List<Event> Events = new List<Event>();
   for (Event e : Trigger.new)
  {    
          Id assigedtoUser=e.OwnerId;
          User createdUser=e.CreatedBy;
           if ( assigedtoUser != createdUser.Id ) 
           {
               CalendarEV__c C = new CalendarEV__c();
               c.Name = e.Subject;
               c.Start__c = e.StartDateTime;
               c.End__c = e.EndDateTime;
               c.Location__c = e.Location;
               c.Description__c = e.Description;
                    
              insert C;
         }
    }
   }
KatherineCKatherineC
The trigger prevents us from creating event in public calendar, but it's ok, we work around other company needs and just use the first version. Thanks so much Ramakrishnan.