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
CertifiedAdminCertifiedAdmin 

How to write a test class for an APEX trigger

I've created the following Trigger to update the Status of an Event to "Rejected" before delete.  Meaning, our sales reps can delete events in SFDC, but we want the status to be changed to "Rejected" when they do this.  We have an active integration which would then send that status to another application, but delete it in SFDC.

 

I am new to APEX and need help creating a test class for this trigger so I can move it to production:

 

1
2
3
4
5
6
7
8
9
10
11
12
//Wrtite Same trigger for your object
trigger updateEventStatus on Event (before delete) 
{
    List<Event> listEvent = new List<Event>();
    for(Event e : [Select id from Event where id in: trigger.old])
    {
        //Change this to your status field API Name 
        e.Status__c = 'Rejected';
        listEvent.add(e);
    }
    update listEvent;
}



 

 

ANY insight would be MUCH appreciated!

 

Best regards,

 

Adam

Best Answer chosen by Admin (Salesforce Developers) 
spraetzspraetz

To cover a trigger with a test class, you simply need to perform the correct DML statement to cause the trigger to fire. 

 

In your case, you would simply create an Event, then delete it, query for it using ALL ROWS (So you can find deleted records) and assert that the status is correct.

 

Something like this:

 

@isTest
private class TestEventTrigger{
    private static testmethod void testStatusOnDelete(){
        Event e = new Event();
        //populate the required fields for event here

        insert e;
        //Start the test (This is done for governor limit reasons, look this up in the documentation if haven't used this before)
        Test.startTest();
        delete e;
        Test.stopTest();
        Event deletedEvent = [SELECT id FROM Event WHERE id = :e.id ALL ROWS];
        //Note: All ROWS will retrieve soft deleted records as well
        System.assertEquals(deletedEvent.status__c, 'Rejected');
    }
}

 

All Answers

Starz26Starz26

Usual Test Class format (if you need helo with then then let us know.)

 

Just create a new instance of Event, set the fields to appropriate values, then insert the event.

 

Once inserted, get the id, then delete the event.

 

Then system.assertequals('Rejected',xxx.Status__c);

 

That should cover all your code.

CertifiedAdminCertifiedAdmin

Thanks for your quick reply....

 

Unfortunately, as all of the APEX development is new to me, I would need some extra help per your comment.

 

Any insight??

 

Thanks,

Adam

spraetzspraetz

To cover a trigger with a test class, you simply need to perform the correct DML statement to cause the trigger to fire. 

 

In your case, you would simply create an Event, then delete it, query for it using ALL ROWS (So you can find deleted records) and assert that the status is correct.

 

Something like this:

 

@isTest
private class TestEventTrigger{
    private static testmethod void testStatusOnDelete(){
        Event e = new Event();
        //populate the required fields for event here

        insert e;
        //Start the test (This is done for governor limit reasons, look this up in the documentation if haven't used this before)
        Test.startTest();
        delete e;
        Test.stopTest();
        Event deletedEvent = [SELECT id FROM Event WHERE id = :e.id ALL ROWS];
        //Note: All ROWS will retrieve soft deleted records as well
        System.assertEquals(deletedEvent.status__c, 'Rejected');
    }
}

 

This was selected as the best answer
Starz26Starz26

NM, see above

CertifiedAdminCertifiedAdmin

Spraetz,

 

Thanks so much for your help with this.  I've been able to write the test class as such:

 

@isTest
private class TestEventTrigger{
    private static testmethod void testStatusOnDelete(){
        Event e = new Event();
        //populate the required fields for event here
        e.Subject = 'call';
        e.Type_of_Activity__c = 'in-person';
        e.Event_Type__c = 'meeting';
        e.Activity_Category__C = 'Secure Start';
        e.WhoID = '003P000000ORS5q';
        e.durationinminutes = 1440;
        e.isalldayevent = true;
        e.activitydate = System.today();   
        insert e;
        //Start the test (This is done for governor limit reasons, look this up in the documentation if haven't used this before)
        Test.startTest();
        delete e;
        Test.stopTest();
        Event deletedEvent = [SELECT id FROM Event WHERE id = :e.id ALL ROWS];
        //Note: All ROWS will retrieve soft deleted records as well
        System.assertEquals(deletedEvent.status__c, 'Rejected');
    }
}

 

 

 

However, when running the test, I'm only receiving 41% coverage due to the following exception:

 

System.QueryException: List has no rows for assignment to SObject

 

 

Any advice on how to fix this one too??

 

Thanks again,

 

Adam