+ Start a Discussion
anvesh@force.comanvesh@force.com 

unable to write for checkrecursive in Test Class ?

I have writtened a Trigger. Used check recursive to stop after the trigger fire.  But when i writeened in Test class the code coverage is not passed. when i remove check recursive it is passed. How to overcome this. please suggest me.

Trigger code :

trigger StageTrigr on Opportunity (before insert,before update) {
if(checkRecursive.runOnce()){
    Boolean bool = true;
    List<Opportunity> OppUp = new List<Opportunity>();
    for(Opportunity c : Trigger.new) {
        if(Trigger.Isupdate){
        Opportunity oldOpp = Trigger.oldMap.get(c.ID);  
        if(oldOpp.StageName!=NULL && oldOpp.StageName == '5-Paperwork/Ticketing incoming'){
            if (c.StageName == '1-Discovery') {            
                    c.StageName = '5-Paperwork/Ticketing incoming';            
            }
            if (c.StageName == '2-Hypo / Illustration delivery') {            
                    c.StageName = '5-Paperwork/Ticketing incoming';            
            }
            if (c.StageName == '3-Rep follow up - VM') {            
                    c.StageName = '5-Paperwork/Ticketing incoming';            
            }
            if (c.StageName == '4-Client Meetings') {            
                    c.StageName = '5-Paperwork/Ticketing incoming';            
            }
            if (c.StageName == '5-Paperwork/Ticketing incoming') {            
                    c.StageName = '5-Paperwork/Ticketing incoming';            
            }
            if (c.StageName == '6 - Close Lost') {            
                    c.StageName = '6 - Close Lost';            
            }
            if (c.StageName == '7 - Close Won') {            
                    c.StageName = '7 - Close Won';            
            }
        }
        if(oldOpp.StageName != '5-Paperwork/Ticketing incoming'){
            if (c.StageName == '5-Paperwork/Ticketing incoming') {            
                    c.StageName = '5-Paperwork/Ticketing incoming';            
            }
            
        }
        if(oldOpp.StageName!=NULL && oldOpp.StageName == '4-Client Meetings'){
            if (c.StageName == '1-Discovery') {            
                    c.StageName = '4-Client Meetings';            
            }
            if (c.StageName == '2-Hypo / Illustration delivery') {            
                    c.StageName = '4-Client Meetings';            
            }
            if (c.StageName == '3-Rep follow up - VM') {            
                    c.StageName = '4-Client Meetings';            
            }
            if (c.StageName == '4-Client Meetings') {            
                    c.StageName = '4-Client Meetings';            
            }            
            if (c.StageName == '6 - Close Lost') {            
                    c.StageName = '6 - Close Lost';            
            }
            if (c.StageName == '7 - Close Won') {            
                    c.StageName = '7 - Close Won';            
            }
        }
        if(oldOpp.StageName != '4-Client Meetings'){
            if (c.StageName == '4-Client Meetings') {            
                    c.StageName = '4-Client Meetings';            
            }
            
        }
        }
        
        
              
        }
        }
    
}

Check Recusrsive Class :

Global Class checkRecursive{
    global static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


Test Class :

@isTest(SeeAllData = false)

private class Test_UpdateStageTrigger
{
public static void resetRunOnceFlag() {
checkRecursive.run = TRUE;
}
static testMethod void UnitTestForException()
{
//checkRecursive.run  = false;

Account ac = new Account ();
ac.name = 'Test Account';
ac.Status__c = 'Active';
ac.Channel__c = 'Bank';
insert ac;

User u = [ select Id from User where id =: UserInfo.getUserId() ];

Territory__c Tr = new Territory__c();
Tr.wholesaler__c = u.id;
Tr.name = 'Test Territory';
Tr.Division__c = 'East';
Tr.Type__c = 'ForeCare';
insert Tr;

Contact c = new Contact();
c.Lastname = 'test';
c.Accountid = ac.id;
c.MailingState = 'CA';
c.MailingCity = 'SAN DIEGO';
c.MailingPostalCode = '92121';
c.Forecare_Territory__c = Tr.id;

insert c;

List<Opportunity> Olist = [select id,name,stagename,closedate from Opportunity where Contact_Name__r.id =:c.id];

Opportunity O = new Opportunity();

O.Name = 'Test Opp';
O.AccountId=ac.Id;
O.StageName='1-Discovery';
O.CloseDate=Date.Today();

insert O;
// System.assert(checkRecursive.runOnce(), 'Recursion check failed. Please review checkRecursive recursion logic');


O.StageName = '5-Paperwork/Ticketing incoming';

Update O;

Opportunity O1 = new Opportunity();

O1.Name = 'Test Opp1';
O1.AccountId=ac.Id;
O1.StageName='2-Hypo / Illustration delivery';
O1.CloseDate=Date.Today()+1;

insert O1;
 //checkRecursive.run  = false;

O1.StageName = '5-Paperwork/Ticketing incoming';

Update O1;


Opportunity O2 = new Opportunity();

O2.Name = 'Test Opp2';
O2.AccountId=ac.Id;
O2.StageName='3-Rep follow up - VM';
O2.CloseDate=Date.Today()+10;



insert O2;
 //checkRecursive.run  = false;
          

O2.StageName = '5-Paperwork/Ticketing incoming';

Update O2;




}
}
sandeep@Salesforcesandeep@Salesforce
You can write simply that toggel the value of boolean. it is easy to access in test class in single execution.