You need to sign in to do that
Don't have an account?
Test Class for Trigger That Creates Opportunity Contact Role
How do I write a test class to check whether a new Opportunity Contact Role was created from a custom field based off of an Opportunity insert or update?
I've written a Trigger (mostly repurposed code) to create an Opportunity Contact Role when an Opportunity is created or updated with a Primary Contact (custom Contact Lookup field). I'm not sure how to write a test class for it, though. The trigger seems to work well enough.
Here's what it does: If a new Opp is created with a Primary Contact (required field), a new OCR is created as a Primary Contact and with a Decision Maker Role. If the Primary Contact is changed, the old OCR is deleted, then the recreated as an OCR with the Influencer Role, while the new Primary Contact is created as the Primary with a Decision Maker Role. If the insert fails, a friendly error message is thrown.
trigger OnOpportunityPrimaryContactRole on Opportunity (after insert, after update) { List<OpportunityContactRole> newCRList = new List<OpportunityContactRole>(); List<OpportunityContactRole> oldCRList = new List<OpportunityContactRole>(); Set<Id> OppId = new Set<Id>(); Set<Id> ContactId = new Set<Id>(); for(Opportunity o: Trigger.new) { //Checks if the Opportunity is being inserted if(Trigger.isInsert) { if(o.Primary_Contact__c != null) { //Creates the new OCR newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE)); } } else if(o.Primary_Contact__c != null && Trigger.oldMap.get(o.Id).Primary_Contact__c != null) { //Gets the Contact and Opportunity Id from the prior values and adds to this set Opportunity oldOppObj=Trigger.oldMap.get(o.Id); OppId.add(OldoppObj.id); ContactId.add(oldOppObj.Primary_Contact__c); //Selects old OCRs if (OppId.size()>0) oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId]; //Deletes old OCRs if (oldCRList.size()>0) delete oldCRList; if(oldOppObj.Primary_Contact__c != o.Primary_Contact__c) { newCRList.add(new OpportunityContactRole (ContactId=o.Primary_Contact__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE)); newCRList.add(new OpportunityContactRole (ContactId=oldOppObj.Primary_Contact__c, OpportunityId=o.Id, Role='Influencer',IsPrimary=False)); } } } try { //Inserts new OCRs if(newCRList.size()>0) insert newCRList; } catch(Exception e) { System.debug(e); trigger.new[0].addError('Uh Oh... A technical error has occurred creating the Opportunity Contact Role. Please email the SFDC admin or try again later.'); } }
Here is the unit test for your trigger, it will give you 90% coverage, see if you can get to 100%. Remember to include additional required fields for Contact and Opportunity, so that it is compliant with your object definition.
@isTest
private class TestOppTrgWContUpd {
static testmethod void TestOpportunityTrgr() {
Test.startTest();
// Create two contacts
Contact ct1 = new Contact();
Contact ct2 = new Contact();
ct1.FirstName = 'Larrys';
ct1.LastName = 'Page';
ct2.FirstName = 'Marc';
ct2.LastName = 'Buymeoff';
Insert ct1;
Insert ct2;
system.debug('Completed Contact Creation');
// Create Opportunity
Opportunity op1 = new Opportunity();
op1.name = 'Opportunity Contact Insertion Test';
op1.CloseDate = Date.today()+2;
op1.StageName = 'Prospecting';
op1.Primary_Contact__c = ct1.Id ;
//insert op1;
Database.SaveResult sr1 = Database.insert(op1, true);
System.assert(sr1.isSuccess());
system.debug('Inserted new opportunity');
// Update Opportunity with new contact
op1.Primary_Contact__c = ct2.Id;
//update op1;
Database.SaveResult sr2 = Database.update(op1, true);
System.assert(sr2.isSuccess());
system.debug('Opportunity updated');
Test.stopTest();
System.assert(sr2.isSuccess());
}
}
All Answers
Here is the unit test for your trigger, it will give you 90% coverage, see if you can get to 100%. Remember to include additional required fields for Contact and Opportunity, so that it is compliant with your object definition.
@isTest
private class TestOppTrgWContUpd {
static testmethod void TestOpportunityTrgr() {
Test.startTest();
// Create two contacts
Contact ct1 = new Contact();
Contact ct2 = new Contact();
ct1.FirstName = 'Larrys';
ct1.LastName = 'Page';
ct2.FirstName = 'Marc';
ct2.LastName = 'Buymeoff';
Insert ct1;
Insert ct2;
system.debug('Completed Contact Creation');
// Create Opportunity
Opportunity op1 = new Opportunity();
op1.name = 'Opportunity Contact Insertion Test';
op1.CloseDate = Date.today()+2;
op1.StageName = 'Prospecting';
op1.Primary_Contact__c = ct1.Id ;
//insert op1;
Database.SaveResult sr1 = Database.insert(op1, true);
System.assert(sr1.isSuccess());
system.debug('Inserted new opportunity');
// Update Opportunity with new contact
op1.Primary_Contact__c = ct2.Id;
//update op1;
Database.SaveResult sr2 = Database.update(op1, true);
System.assert(sr2.isSuccess());
system.debug('Opportunity updated');
Test.stopTest();
System.assert(sr2.isSuccess());
}
}
Awesome! Thanks a lot. I used a variation of that code to get 90% coverage as you mentioned. Works great!
One more question: how do I get to 100%? I have to cover the exception, but I'm not sure how, to be honest. What condition can I create in the test class to hit that exception and get that 100%?
This is the uncovered portion of the trigger: