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
LaurenP6777LaurenP6777 

Apex Test Class Help for Simple Trigger

Hi guys, 

 

I am having trouble with a test class for a simple trigger. My test class has no coverage and I am very new when it comes to test classes.

 

My trigger is pretty basic: When a user creates a Trip Report (custom object :Trip_Report__c), a related record is automatically created that adds the Trip Report owner to the list of meeting attendees (custom object: covance_attendees__c). 

 

 

 

trigger createCovanceAttendeeonnewTR on Trip_Report__c (after insert) {

List <Covance_Attendees__c> attToInsert = new List <Covance_Attendees__c> ();

for(Trip_Report__c t: Trigger.new) {

if (t.name<>'1') {

Covance_Attendees__c a = new Covance_Attendees__c (); 

a.Attendee_Name__c = t.OwnerId;
a.How_Attended__c = 'In Person';
a.Trip_Report__c = t.id;

attToInsert.add(a);
        
        
        }//end if
        
    }//end for o
    
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert attToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
    
}

 

 

 

My Test Class has 0% coverage. I'm not sure what I am doing wrong as I don't know Apex very well.

 

If anyone knows how to fix this code- could you actually "write" the code instead of just telling me how to fix it? If I could see a proper test class in a context I understand, I think it will be a huge help. THANK YOU!!!


 

public class test_createCovanceAttendeeonnewTR
{
public static testMethod void testcreateCovanceAttendeeonnewTR()
{
//Insert Trip_Report__c
try
{
Trip_Report__c t = new Trip_Report__c(Name='TestMethodMouse1',Call_Objective__c='Follow Up',Visit_Date__c =System.Today());
insert t;

Test.StartTest();
Insert t;
Test.StopTest();



t=[SELECT Name,Trip_Description__c FROM Trip_Report__c WHERE Id = :t.Id];
system.assert (t.Name <> '1');
}
catch (System.DmlException e)
{
System.assert(true);
}
}
}


Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Did you happen to get any error message when you run the test.?? If you have any validation rules on the Trip_report__c object then make sure that the condition is satisfied when you insert the record in the test class.

 

Thanks

Sam

All Answers

chris_centrachris_centra

Hello.

 

Have you confrimed (via debugging or assets) that your Trip_Report__c record is indeed being created?  As long as the record is successfully created, it should have coverage better than 0%.  Could it be failiing on a validation rule or required field?  i would start by checking the debug log when you run your test coverage - or use asserts to confirm that the record is created....

 

(note that you'll probably get an error for trying to insert the same record twice - because after the first insert, the Id will be populated.)

 

thanks

chris

jungleeejungleee

Hi ,

 

i think the problem was, you were not using the @isTest annotation in the test class. try the below class.

 

@isTest(SeeAllData=true)

public class test_createCovanceAttendeeonnewTR
{
public static testMethod void testcreateCovanceAttendeeonnewTR()
{
//Insert Trip_Report__c

Trip_Report__c t = new Trip_Report__c(Name='TestMethodMouse1',Call_Object

ive__c='Follow Up',Visit_Date__c =System.Today());
insert t;

}

}

regards

Sam

LaurenP6777LaurenP6777

Coverage still shows 0% when I click "Run Test".

jungleeejungleee

Did you happen to get any error message when you run the test.?? If you have any validation rules on the Trip_report__c object then make sure that the condition is satisfied when you insert the record in the test class.

 

Thanks

Sam

This was selected as the best answer