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
pearlbearpearlbear 

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.

Always ThinkinAlways Thinkin

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

pearlbearpearlbear

Here's the trigger:

trigger SendEmailTrigger on Opportunity (after insert) {
	/* This trigger is to send a thank you note when a donation 
	   is entered. */
	   
	if (Opportunity.Amount != null) { //There is some amount

		// Getting contact data
		for (Opportunity o : Trigger.new) {
			//Trigger.new has: Amount, CloseDate, Description, CampaignId, Fund_Allocation_Notes__c in Opportunity object
			
			String cid = [SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId = :o.id limit 1].ContactId;
			for (Contact c : [SELECT Contact.Email, Contact.FirstName, Contact.LastName FROM Contact WHERE ID = :cid]) {
				
				String[] toAddress = new String[] {c.Email};  // if empty, a task will be created instead
				
				Decimal donation = o.Amount.round();
				String emailMsg = 
				'Hello ' + c.FirstName + ',\n' +
				'Thank you for your donation of $' + donation + '\n' + 
				'Sincerely,\n Ravenswood Education Foundation';
	
				try { //Send that email:
					sendMail.mailSend(toAddress, emailMsg);		
					//set thank you note
					// Opportunity newOpp = new Opportunity(Id = o.id, Thank_You_Note__c = TRUE);
					// Insert newOpp;
				} catch (Exception e) { 
					// If no valid email - Put in an activity
					Task t = new Task (WhatId = o.id, ActivityDate = System.today(), WhoId = cid, Subject = 'Send Letter', Description = 'Send Thank You Note via Mail, no email on file for donor');
					Insert t;
				}
			}
		}
		
		
	}
}

The challenge around testing is finding out how to create a contact, then the opportunity contact role.

 

 

pearlbearpearlbear

I added some code to insert a contact before the opportunity, and it still failed at the opportunity insert as before...