You need to sign in to do that
Don't have an account?
Jos 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.
However ...
Opportunity OldContact = trigger.oldmap.get(Opp.Contact__c);
If (Opp.Contact__c == OldContact.Id){
are never touched ....
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 ....
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
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/
I guess it was just too late. The final result is 98% coverage.
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 link. https://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" :
What is the last red line? (the missing 2%)