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
Jos VervoornJos Vervoorn 

Why is my test class not touching this code

HI, I have a 'simple' trigger on the opportunity which creates new contact role entry whenever the Contact__c is changed. So the trigger is Before Update.
 
If(Trigger.IsUpdate){ //** 18/12/2017 - Changed from Insert to Update.. 
        For (Opportunity Opp: Trigger.new){
            if (Trigger.oldMap.get(opp.Contact__c) != null) { //** So we have a contact !!
               Opportunity OldContact = trigger.oldmap.get(Opp.Contact__c);
               If (Opp.Contact__c == OldContact.Id){
                   System.debug('** * ** ||AutoSetOpportunityLookupValues.Trigger - Old Contact....:'+OldContact);
                   System.debug('** * ** ||AutoSetOpportunityLookupValues.Trigger - New Contact....:'+Opp.Contact__c);
               }
            }
        }
So at first in my unit test I create opportunity without contact. All is fine. Then I perform several updates. One of these is to actually update the Contact__c with a value (contact Id) as it's a lookup.
NewQualificationOpportunity.VAT_Included__c = false;
         NewQualificationOpportunity.Contact__c = NewContactII.id;
         NewQualificationOpportunity.StageName='Written Approval';
         Update NewQualificationOpportunity;

However ... 

               Opportunity OldContact = trigger.oldmap.get(Opp.Contact__c);
               If (Opp.Contact__c == OldContact.Id){

are never touched ....

 
Best Answer chosen by Jos Vervoorn
Alain CabonAlain Cabon
Hi Jos,

It is a trigger on the object opportunity so the objects in the maps are also opportunities (and not contacts).

for (Opportunity opp : Trigger.new) {
// Access the "old" record by its ID in Trigger.oldMap
   if (Trigger.oldMap.get(opp.Id) != null) {
     Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
     Contact__c OldContact = oldOpp.Contact__c;  // not tested

http://www.sfdc99.com/2014/02/25/comparing-old-and-new-values-in-a-trigger/
 

All Answers

Alain CabonAlain Cabon
Hi Jos,

It is a trigger on the object opportunity so the objects in the maps are also opportunities (and not contacts).

for (Opportunity opp : Trigger.new) {
// Access the "old" record by its ID in Trigger.oldMap
   if (Trigger.oldMap.get(opp.Id) != null) {
     Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
     Contact__c OldContact = oldOpp.Contact__c;  // not tested

http://www.sfdc99.com/2014/02/25/comparing-old-and-new-values-in-a-trigger/
 
This was selected as the best answer
Raj VakatiRaj Vakati
Can you share the complete test class and try this code
 
NewQualificationOpportunity.VAT_Included__c = false;
         NewQualificationOpportunity.StageName='Written Approval';
         Update NewQualificationOpportunity;

 
Jos VervoornJos Vervoorn
Thanks Alain,

I guess it was just too late. The final result is 98% coverage.
For (Opportunity Opp: Trigger.new){
           
               Opportunity OldOpp = trigger.oldmap.get(Opp.id);
               Id OldContact = oldOpp.Contact__c;
              If (Opp.Contact__c == OldContact){
                  System.debug('** * ** ||AutoSetOpportunityLookupValues.Trigger - Old Contact....:'+OldContact);
                   System.debug('** * ** ||AutoSetOpportunityLookupValues.Trigger - New Contact....:'+Opp.Contact__c);
               }
            
        }


 
Alain CabonAlain Cabon
Hi Jos,

The final result of your test is "already an excellent"' 98% coverage ( 75% needed).
There is probably just one line not covered.

Open your trigger in the developer console.

To open the Developer Console, click Your Name | Developer Console or navigate to Setup | Develop | Tools and click the Developer Console linkhttps://developer.salesforce.com/page/Developer_Console

And open your tested trigger. You wiill found a drop list on the upper left iside, choose "All Tests" : 

User-added image

What is the last red line? (the missing 2%)