+ Start a Discussion
darcusmietzdarcusmietz 

Test Class for OpportunityContactRole Trigger

I am in need of help with a Test class for my trigger. I'm getting zero test code coverage, because I am an absolute newbie at Apex. 

 

My first question is, do you recommend reading the entire documentation online, or are there any decent books out there regarding Apex code?

 

My second question is, How can I increase my code coverage on the test class below?

 

This is the trigger: 

trigger UpdateContactLeadSource on Opportunity (before update) {
    /*  delete this first part of code and in the query instead of ":oppsToFill" bind to ":trigger.new" or
        ":trigger.newMap.keyset()".
    */
    Set<Id> oppsToFill = new Set<Id>();
    
    for(Opportunity o : trigger.new){
        if(o.LeadSource <> null) {
           oppsToFill.add(o.Id);
      }
    }

    // Now we'll select all possible contacts wasting only 1 query.
    if(!oppsToFill.isEmpty()){
        List<OpportunityContactRole> roles = [SELECT OpportunityId, Contact.Name, Contact.LeadSource
            FROM OpportunityContactRole
            WHERE isPrimary = true AND Contact.LeadSource != null AND OpportunityId IN :oppsToFill];

        if(!roles.isEmpty()){
            for(OpportunityContactRole ocr : roles){
                Opportunity oppToBeFilled = trigger.newMap.get(ocr.OpportunityId);
                System.debug('Changing lead source on ' + oppToBeFilled.Name + ' from ' + oppToBeFilled.LeadSource + ' to ' + ocr.Contact.LeadSource + ' (thx to ' + ocr.Contact.Name + ' being the Primary Contact).');
                oppToBeFilled.LeadSource = ocr.Contact.LeadSource;
            }
        }
    }
   
    // NEEDS TO INCLUDE IF STATEMENT IN CASE NO PRIMARY CONTACT IS LISTED
}

 

This is the Test Class I have so far 

@isTest (SeeAllData = true) 
public with sharing class TestPrimaryContactLeadSrc {

 static testMethod void TestPrimaryContactLeadSrc() 
      {  
       		   Set<Id> oppsToFill = new Set<Id>();
       		   Contact newContact = new Contact( lastname= 'Testerson',
                                        LeadSource = 'Cold Call'
                                       // WhatId = newOpp.id 
                                        );                
                insert newContact;
       		
       		
               Opportunity o = new Opportunity (Name = 'testopp',
               										Set_up_Fee__c = 10, 
               										Term__c = 1,
               										CARR__c = 1,
               										StageName = 'Meeting',
               										ForecastCategoryName = 'Pipeline',
               										CloseDate = date.today(),
               										Type = 'Upsell',
               										LeadSource = 'Sales'
               										//WhatId = Contact.Id
               										);
               									
               insert o;
               
               
               OpportunityContactRole ocr=new OpportunityContactRole(Role='Decision Maker',OpportunityId=o.Id,ContactId=NewContact.Id,Isprimary=true);
     insert ocr;
               
               
                Task newTask = new Task(Description = 'Survey Transaction',
                                        Priority = 'Normal', 
                                        Status = 'Inbound Email', 
                                        Subject = 'Other', 
                                        IsReminderSet = true, 
                                        ReminderDateTime = System.now()+1,
                                        WhatId = o.id 
                                        );                
                insert newTask;
                

                
    }
    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Coco_SdyneyCoco_Sdyney

In your test class, first you need to create a Account , as test contact and test opportunity both need it.

Then after created OCR, should update test opportunity to fire trigger so get your trigger tested.

 

Do the code something as below:

 

Account a = new Account;

a.Name = 'testing in test method';

insert a;

 

Contact newContact = new Contact(Lastname='testLastName', AccountId = a.Id, LeadSource='Cold call');

insert newContact;

 

Opportunity o = new Opportunity(AccountId = a. Id, stagename='Meeting',Set_up_fee__c=10, ...);

insert o;

 

OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = o.Id, ContactId = newContact.Id, ...);

insert ocr;

 

o.Set_up_fee__c = 9;

update o;

 

 

All Answers

Coco_SdyneyCoco_Sdyney

In your test class, first you need to create a Account , as test contact and test opportunity both need it.

Then after created OCR, should update test opportunity to fire trigger so get your trigger tested.

 

Do the code something as below:

 

Account a = new Account;

a.Name = 'testing in test method';

insert a;

 

Contact newContact = new Contact(Lastname='testLastName', AccountId = a.Id, LeadSource='Cold call');

insert newContact;

 

Opportunity o = new Opportunity(AccountId = a. Id, stagename='Meeting',Set_up_fee__c=10, ...);

insert o;

 

OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = o.Id, ContactId = newContact.Id, ...);

insert ocr;

 

o.Set_up_fee__c = 9;

update o;

 

 

This was selected as the best answer
darcusmietzdarcusmietz

Didn't realize I had to create an Account first, but that makes perfect sense in retrospect. I'm the epitome of an amateur.

 

Thanks for your quick guidance.