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
peterpptpeterppt 

Help to get Trigger Coverage

I have (with Forum help !) created a simple Trigger which works beautifully.

 

trigger Peter1 on Event (before insert) {
   if (trigger.isBefore && trigger.isInsert) {
     
      for (Event e : trigger.new) {
          if ( e.public_name__c == 'Hennock' ) {
                e.OwnerId = '023b000000098Dx';
         }
      }
   }
   // All done!
}

 I have ( again with forum help ) got the following test code to run

 

@isTest
public class PeterTest2 {
     static testMethod void Testx () {
         
        Event objEvent = new Event();
        objEvent.Subject = 'Test666';
        objEvent.OwnerId = '005b0000000M1pvAAC';
        objEvent.StartDateTime = datetime.newInstance(2012, 10, 1, 12, 30, 0);
        objEvent.EndDateTime = datetime.newInstance(2012, 10, 1, 13, 30, 0);
        //objEvent.createdbyid = '005b0000000M1pvAAC';
        objEvent.Public_Name__c = 'Hennock';

        try {
    
        insert objEvent;
        } catch (DmlException e) {
        // Process exception here 
    
        }   
         
     } 

     }

But when I run this it gives Test Coverage : None.   I'm puzzled why my test code is not firing the trigger.  The key is the custom field  Public_Name__c =  'Hennock'  and that is an insert value in the test code.  If you create an event with this value through the user interface then the triggrer fires.

 

If anyone can help me solve this then I think I'm ready to deploy !  Thanks.

Rajesh SriramuluRajesh Sriramulu

Hi,

 

One thing is u have hard coded the both in trigger and test class that is not right.

U just change the owner in test class as the owner in trigger.

 

Regards,

Rajesh.

 

 

 

 

peterpptpeterppt

Hi - thanks, but the  OwnerId's have to be different.  The trigger's purpose is to change the OwnerId from that of the logged on user to that of a public calendar.   I tried  making them the same to test your point, even though this would defeat the object of the trigger,  but the coverage is still none.

 

 

Rajesh SriramuluRajesh Sriramulu

Hi,

 

I tried with different ownerid in trigger and test class, when I run this test class it chowing 100% code coverage.

But u keep like this isTest(SeeAllData=true) and Try .

 

Regards,

Rajesh

 

 

 

peterpptpeterppt

Thanks very much for running some code.

 

I must apologise - when I said I was getting Coverage None I was looking at the button above the code window Code Coverage:None.  I thought the legend would change.  I really must read the Apex Developer console user guide !

 

But I should have been looking at the Overal Code Coverage panel !  As you rightly say Code coverage = 100%. 

 

I added SeeAllData = True - but it wasn't necessary - it gives 100% even when I revert to my original code !

 

Very sorry !  Thanks for your patience - I've learned a lot !

 

Peter