+ Start a Discussion
JN22JN22 

Test Code Coverage

Hello,

I have a trigger and test class below.  I am only getting 7% coverage on my trigger and I don't understand why.  Can anyone help?

Trigger:
trigger UpdateConsultOpp on Opportunity (before insert, before update) {   

if(checkRecursiveBI.runOnceBI()||checkRecursiveBU.runOnceBU())
{
   
    Set<ID> setOppIds = new Set<ID>();
/*    List<Opportunity> opp1 = [SELECT Id
                             FROM Opportunity
                             WHERE Id IN: Trigger.newMap.keySet()]; 
                                     
    for (Opportunity opp :opp1){*/
    for(Opportunity opp:Trigger.new){
        setOppIds.add(opp.Id);
    }
    Map<ID, Opportunity> mapAcc = new Map<ID, Opportunity>([Select Id, Account.Consultant_Partner_Primary__c, Account.Consultant_Partner_Secondary__c, Consultant_Type__c
                                                            FROM Opportunity
                                                            WHERE Id in:setOppIds]);
    if(mapAcc.size()>0){
    List<Opportunity> opp2 = [SELECT Id,Consultant_Type__c,Consultant_Partner__c
                             FROM Opportunity
                             WHERE Id IN: Trigger.new]; 
                                     
        for (Opportunity opp :opp2){
//        for(Opportunity opp:Trigger.New){
            IF(mapAcc.containsKey(opp.Id) && opp.Consultant_Type__c == 'Primary Consultant'){
                opp.Consultant_Partner__c = mapAcc.get(opp.Id).Account.Consultant_Partner_Primary__c;
            }
            ELSE IF(mapAcc.containsKey(opp.Id) && opp.Consultant_Type__c == 'Secondary Consultant'){
                opp.Consultant_Partner__c = mapAcc.get(opp.Id).Account.Consultant_Partner_Secondary__c;
            }
        }
    }
}

}

Test Class:
@isTest private class TestOppConsultants {

    @isTest private static void testOppConsults() {
       
    Account acct1 = TestAcctTeamCreate.createAcct(0);
    insert acct1;
        
    Account acct2 = TestConsultAcctCreate.createConsult1(0);
    insert acct2;
  
    Account acct3 = TestConsultAcctCreate.createConsult2(0);
    insert acct3;
  
    Opportunity opp1 = TestOppCreate3.createOpp(acct1.Id);
    insert opp1;

        Account acctPrim = [SELECT Id, Consultant_Partner_Primary__c
                            FROM Account
                            WHERE Id =: acct1.Id];
            acctPrim.Consultant_Partner_Primary__c = acct2.Id;
        update acctPrim;

        
        Opportunity oppPrim = [SELECT Id, Consultant_Type__c
                                FROM Opportunity
                                WHERE Id =: opp1.Id];
            oppPrim.Consultant_Type__c = 'Primary Consultant';
        update oppPrim;
            

        Account acctPrim1 = [SELECT Id, Consultant_Partner_Primary__c
                            FROM Account
                            WHERE Id =: acct1.Id];
            acctPrim1.Consultant_Partner_Primary__c = acct3.Id;
        update acctPrim1;


        Account acctSec = [SELECT Id, Consultant_Partner_Secondary__c
                           FROM Account
                           WHERE Id =: acct1.Id];
            acctSec.Consultant_Partner_Secondary__c = acct3.Id;
        update acctSec;

        
        Opportunity oppSec = [SELECT Id, Consultant_Type__c
                                FROM Opportunity
                                WHERE Id =: opp1.Id];
            oppSec.Consultant_Type__c = 'Secondary Consultant';
        update oppSec;


        Account acctSec1 = [SELECT Id, Consultant_Partner_Secondary__c
                           FROM Account
                           WHERE Id =: acct1.Id];
            acctSec1.Consultant_Partner_Secondary__c = acct2.Id;
        update acctSec1;

        
    Test.startTest();
        update acct1;
        update acct2;
        update acct3;
        update opp1;
        update acctPrim;
        update oppPrim;
        update acctPrim1;
        update acctSec;    
        update oppSec;    
        update acctSec1;    
    Test.stopTest();
    }
}



DazedPuzzledDazedPuzzled
Are you setting this condition:- if(checkRecursiveBI.runOnceBI()||checkRecursiveBU.runOnceBU()) to true?
JN22JN22
I have a class that calls that to prevent triggers from multiple fires:

public Class checkRecursiveBI{
    private static boolean run = true;
    public static boolean runOnceBI(){
    if(run){
     run=false;
     return true;
    }
    else{
        return run;
    }
    }
}


DazedPuzzledDazedPuzzled
Can you try by commenting 60, 61, 62,63 these four lines
JN22JN22
I tried, but it had no effect.