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
case commentscase comments 

help with test code

My code tracks how long a case has been in a certain picklist value.  There is a custom object called "Days In Status" that captures when a picklist is changed and logs in how long it was in that picklist.  I then have a rollup field back to the case to track the total days.  When I run the scenarios in the sandbox org, everything works fine.  When I move the change a picklist, the trigger fires and I get a new record in my custom object.  

 

When I write my test code, It seems as if the trigger is not firing.  Where did I go wrong?

 

Trigger:

trigger AfterCaseTrigger on Case (after insert, after update) {

    List<Days_in_Status__c>DaysStatus = new List<Days_in_Status__c>();
    Days_in_Status__c dis = new Days_in_Status__c();
    for(Case caseObj : Trigger.new){
        
            //Days in Stage code
            if(checkRecursive.runOnce()){
                if(oldCase.Major_Project_Status_Indicator__c == 'Orange' && caseObj.Major_Project_Status_Indicator__c != 'Orange' ){
                    
                    //get the number of days between today and the date the start date orange was
                    dis.Status__c = 'Orange';
                    if(caseObj.Date_to_Orange__c != null){
                        dis.Days_in_Status__c = caseObj.Date_to_Orange__c.daysBetween(system.today());}
                    dis.Case__c = caseObj.ID;
                    
                    //insert those days
                    DaysStatus.add(dis);
                }
                if(oldCase.Major_Project_Status_Indicator__c == 'Red' && caseObj.Major_Project_Status_Indicator__c != 'Red' ){
                    
                    //get the number of days between today and the date the start date red was
                    dis.Status__c = 'Red';
                    if(caseObj.Date_to_Red__c != null){
                        dis.Days_in_Status__c = caseObj.Date_to_Red__c.daysBetween(system.today());}
                    dis.Case__c = caseObj.ID;
                    
                    //insert those days
                    DaysStatus.add(dis);
                }
                if(oldCase.Internal_Dev_Status__c == 'PxM Review' && caseObj.Internal_Dev_Status__c != 'PxM Review' ){
                    
                    //get the number of days between today and the date the start date PxM Review was
                    dis.Status__c = 'PxM Review';
                    if(caseObj.Date_to_PxM_Review__c != null){
                        dis.Days_in_Status__c = caseObj.Date_to_PxM_Review__c.daysBetween(system.today());}
                    dis.Case__c = caseObj.ID;
                    
                    //insert those days
                    DaysStatus.add(dis);
                }
            }
 
    
    if(DaysStatus.isEmpty()==false){
        insert DaysStatus;}
    
    
    
    
}

 Recursive class:

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

 Test Class:

@isTest
private class CaseTriggerTestClass {
    static testMethod void runTest()
    {
        test.starttest();

        Case case1 = new Case();
        case1.Major_Project_Status_Indicator__c = 'Orange'; 
        case1.Internal_Dev_Status__c = 'Open';
        insert case1;
        System.debug('Date to Orange1:' + case1.Date_to_Orange__c);
        case1.Date_to_Orange__c = date.newInstance(2013,1,1);
        case1.Major_Project_Status_Indicator__c = 'Red';
        case1.Internal_Dev_Status__c = 'PxM Review';
        update case1;
        System.debug('Date to Orange2: ' + case1.Date_to_Orange__c);
        System.debug('Days in orange: ' + case1.Days_in_Orange__c);
		System.debug('Major Project Status Indicator: ' + case1.Major_Project_Status_Indicator__c);
      
        
        List<Days_in_Status__c> ds = new List<Days_in_Status__c>();
        
        test.stoptest();
        
        ds = [SELECT Name, Case__c, Days_in_Status__c, Status__c FROM Days_in_Status__c];
		system.debug('Size of List: ' + ds.size());
        
        }
       
    
}

 

From my debug logs, I can see that the Major Project Status Indicator did move to Red on the update.  That should have fired the trigger.  But size of my list comes back at 0.

Any help is greatly appreciated.  Thanks.

chris_centrachris_centra

i think you need to reset the "run" value in your checkrecursive class after you insert but before you update your case.

chris