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
Liz MalmanLiz Malman 

Apex Trigger is not 100% covered

Hi Salesforce Community,

I created a test class that I believe should cover the trigger I created 100% but I'm only getting 64% coverage. I know I don't need 100% coverage to bring this trigger and class into my production but I'd like to figure this out.

Here is my code for my trigger:
trigger Renewal_PSM on Opportunity (after insert, after update) {

    Set<Id> oppIds = new Set<Id>();
    Map<Id,Id> oppIds2 = new Map<Id,Id>();
    List<Opportunity> opptoUpdate = new List<Opportunity>();
    
    for(Opportunity o : Trigger.new){
         if(o.Previous_Renewal_Opportunity__c != null)
              oppIds.add(o.Id);
              oppIds2.put(o.Previous_Renewal_Opportunity__c,o.Id);
    }
    
    for(Opportunity opp2: [Select Amount from Opportunity where Id in: oppIds2.keySet()])
    {
        if(opp2.Amount !=null && !checkRecursive.SetOfIDs.contains(opp2.Id))
        {
          Opportunity o1= new Opportunity();
          o1.id=oppIds2.get(opp2.Id);
          checkRecursive.SetofIDs.add(opp2.Id);
          o1.Previous_Renewal_Product_Sum_Amount__c =opp2.Amount;
          opptoUpdate.add(o1);
        }
    
    }

if(opptoUpdate.size()>0)
update opptoUpdate;
}

Here is the code for my test class:
@isTest
private class Test_Renewal_Product_Sum_Amount{
  
         
  static testMethod void test_OpportunityTrigger2(){
  
   Account account_Obj = new Account(Name = 'Name123');
    Insert account_Obj; 

   
    Opportunity opp = new opportunity(Name = 'Name321',Description='Testing', StageName = 'Prospecting',AccountId=account_Obj.Id, CloseDate = Date.today());
  Insert opp;
      
   
   
    Opportunity opportunity_Obj = new Opportunity(IsPrivate = false, Description='Testing', Name = 'Name198',AccountId=account_Obj.Id,StageName = 'Proposal', previous_Renewal_Opportunity__c=opp.id, CloseDate = Date.today());
    Insert opportunity_Obj;
      
      Opportunity opportunity_Obj2 = new Opportunity(IsPrivate = false, Description='Testing', Name = 'Name196',AccountId=account_Obj.Id,Amount=123456, StageName = 'Discovery', CloseDate = Date.today());
      
      Insert opportunity_Obj2;
      
      opp.previous_Renewal_Opportunity__c= opportunity_Obj2.Id;
      update opp;
  
  }
}

Thanks,
Liz
Etienne DKEtienne DK
Hi Liz, forgive me if you already know this .. are you aware of the colour coded coverage which shows you what has or has not run.
It makes it easy to see what is not being hit by the test class.
To get to the colour code coverage screen, run the test and then click the class/trigger in the 'Overall Code Coverage' windows, shown bottom right

I didn't delve into your code but did notice you are not using Test.StartTest()  / StopTest() around DML operations - not critical but you may want to check if needed
Code coverage - blue is tested, red not tested
Liz MalmanLiz Malman
Yes, I'm aware of the color coding when testing. 

The following lines with * next to them come up as red. But I'm not sure why because I beleieve they should be covered based on my test class.

trigger Renewal_PSM on Opportunity (after insert, after update) {

    Set<Id> oppIds = new Set<Id>();
    Map<Id,Id> oppIds2 = new Map<Id,Id>();
    List<Opportunity> opptoUpdate = new List<Opportunity>();
    
    for(Opportunity o : Trigger.new){
         if(o.Previous_Renewal_Opportunity__c != null)
              oppIds.add(o.Id);
              oppIds2.put(o.Previous_Renewal_Opportunity__c,o.Id);
    }
    
    for(Opportunity opp2: [Select Amount from Opportunity where Id in: oppIds2.keySet()])
    {
        if(opp2.Amount !=null && !checkRecursive.SetOfIDs.contains(opp2.Id))
        {
          *Opportunity o1= new Opportunity();
          *o1.id=oppIds2.get(opp2.Id);
          *checkRecursive.SetofIDs.add(opp2.Id);
          *o1.Previous_Renewal_Product_Sum_Amount__c =opp2.Amount;
          *opptoUpdate.add(o1);
        }
    
    }

if(opptoUpdate.size()>0)
*update opptoUpdate;
}
Etienne DKEtienne DK
A quick look through and it's not immediately obvious to me why the IF is failing.
I couldn't follow past '!checkRecursive.SetOfIDs.contains(opp2.Id)'
I'd narrow it down by cutting out the checkRecursive check and adding some system.debugs to see why its not getting past the IF