You need to sign in to do that
Don't have an account?
Trigger test class
I've got a test class for a trigger, and I'm running into trouble. First issue: I'm running into an error I can't figure out:
The trigger is for a new opportunity created from a contact record. The trigger is "After Insert" so it's possible to grab a contact id from the OpportunityContactRole record created.
This trigger works great in the sandbox. If the contact has an email, one is sent, if not, a new task is created.
Here's the test class so far:
public class testEmailTrigger { //This class is simply to test the SendEmailTrigger trigger //Test Method static testMethod void testEmail() { //Insert a record into the right table to test //In this case, the table is Opportunity Opportunity o = new Opportunity (); o.Amount=100; o.CloseDate = System.Today(); o.Description = 'TEST'; o.Name = 'TEST'; o.StageName = 'Closed Won'; //Start the test test.startTest(); insert o; test.stopTest(); } }
The error I'm getting is that it "CANNOT_INSERT_UPDATE_ACTIVITY_ENTITY" - I get a "list has no rows for assigment to sObject"
I'm also a bit at a loss as to how to get better test coverage for the trigger. It's a really simple trigger - query of the OpportunityContactRole for contact id, if there is one, send an email, if not, set a new task associated with that opportunity and contact.
Thanks for any guidance. The docs on testing are not very helpful.
Hi pearlbear,
For the testing conditions, you may need to create a test Contact as well as the test Opportunity and then reference its ID when you set the Opportunity values (e.g. o.Contact__c = c.Id). Please post your trigger as well, that will help us helpers.
There's a great article in the wiki about testing:
How to write good unit tests
Here's the trigger:
The challenge around testing is finding out how to create a contact, then the opportunity contact role.
I added some code to insert a contact before the opportunity, and it still failed at the opportunity insert as before...