+ Start a Discussion
Matthew HoldgateMatthew Holdgate 

Test Class picking up mull value but system debug another

Hi All

I am really struggling to find my error here. 

My Apex Class is 
Global class DOUM_WaitingOnApprovals implements Schedulable{
    
    Global static Void ListDOUs() {
        
        List<DOUM_DOU__c> Waitings = [SELECT Id, Name, DOUM_Req_BMgr__r.Email, DOUM_Requester_IT_Mgr__r.Email, DOUM_Domain_Lead__r.Email, DOUM_IT_Domain_Lead__r.Email, DOUM_DOU__c.DOUM_DOU_Approval_Status__c, DOUM_DOU__c.LastModifiedDate, DOUM_DOU__c.DOUM_Req_BMgr__c, DOUM_DOU__c.DOUM_Requester_IT_Mgr__c, DOUM_DOU__c.DOUM_Domain_Lead__c, DOUM_DOU__c.DOUM_WAF__c, DOUM_DOU__c.DOUM_IT_Domain_Lead__c, DOUM_DOU__c.DOUM_BM_Approval__c, DOUM_DOU__c.DOUM_ITM_Approval__c, DOUM_DOU__c.DOUM_DL_Approval__c, DOUM_DOU__c.DOUM_ITDL_Approval__c FROM DOUM_DOU__c WHERE DOUM_DOU_Approval_Status__c = 'In Approval'  AND  LastModifiedDate >: System.now()-3];
        
        
        String Email;
        Boolean BM;
        Boolean ITM;
        Boolean DL;
        Boolean ITDL;
        
        for(DOUM_DOU__c rec : Waitings){
            
            String Name = rec.Name;
            System.debug('Name: '+Name);
            
            IF(String.ValueOf(rec.DOUM_Req_BMgr__c) == Null){ 
                BM = TRUE ;
            }
            ELSE{ 
                IF(rec.DOUM_BM_Approval__c == TRUE){ BM = TRUE; }
                ELSE{ BM = FALSE; }
            }
            
            System.debug('BM'+BM);
            
            IF(String.ValueOf(rec.DOUM_Requester_IT_Mgr__c) == Null){ 
                ITM = TRUE ;
            }
            ELSE{ 
                IF(rec.DOUM_ITM_Approval__c == TRUE){ ITM = TRUE; }
                ELSE{ ITM = FALSE; }
            }
            
            System.debug('ITM'+ITM);
            
            IF(String.ValueOf(rec.DOUM_Domain_Lead__c) == Null){ 
                DL = TRUE ;
            }
            ELSE{ 
                IF(rec.DOUM_DL_Approval__c == TRUE){ DL = TRUE; }
                ELSE{ DL = FALSE; }
            }
            
            System.debug('DL'+DL);
            
            IF(String.ValueOf(rec.DOUM_IT_Domain_Lead__c) == Null){ 
                ITDL = TRUE ;
            }
            ELSE{ 
                IF(rec.DOUM_ITDL_Approval__c == TRUE){ ITDL = TRUE; }
                ELSE{ ITDL = FALSE; }
            }
            
            System.debug('ITDL'+ITDL);
        
            IF( BM == FALSE ){ Email = rec.DOUM_Req_BMgr__r.Email ; System.debug('BM IF');} 
            ELSE IF(ITM == FALSE ){ Email = rec.DOUM_Requester_IT_Mgr__r.Email ; System.debug('ITM IF');}
            ELSE IF(DL == FALSE ){ Email = rec.DOUM_Domain_Lead__r.Email ; System.debug('DL IF');}
            ELSE IF(ITDL == FALSE ){ Email = rec.DOUM_IT_Domain_Lead__r.Email ; System.debug('ITDL IF');} 
            ELSE { break; }
            
            System.debug('Email'+Email);
        
            rec.DOUM_WAF__c = Email ;
            
            System.debug('WAF:'+rec.DOUM_WAF__c);
        
            update rec;
            
            System.debug('After Update WAF'+rec.DOUM_WAF__c);
        }
    }   
    
       global void execute(SchedulableContext sc) {
    
        DOUM_WaitingOnApprovals WA = new DOUM_WaitingOnApprovals();
        
        String DOU = '0 0 0 ? * MON-FRI';
        String jobID = System.schedule('DOUM_WaitingOnApprovals', DOU, WA);

        
   }
}
It pulls the list of records where their approval status is "In Approval" and was edited more than 3 days ago. When it finds a record it does some other checks and populates an email field with a user email. It works when I run it from a button but when writing the test class it works in the debug correctly but fails the assertion checks.

My Test Class is 
@isTest
public class TEST_DOUM_WaitingOnApprovals {
	static testMethod void TestWoA(){
    Profile profileId = [SELECT Id FROM Profile WHERE Name='GSO Basic User_Platform'];
        User u = new user();
        u.LastName = 'Test Coe';
        u.Email = 'test1@test.com';
        u.Alias = 'Tcode';
        u.Username = 'test1234fdfdfefe4trfd34@test.com';
        u.CommunityNickname = 'test122';
        u.LocaleSidKey = 'en_US';
        u.ProfileId = profileId.Id;
        u.TimeZoneSidKey = 'GMT';
        u.LanguageLocaleKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';
        insert u;
        
        User uBM = new user();
        uBM.LastName = 'Test BM';
        uBM.Email = 'testBM@test.com';
        uBM.Alias = 'BM';
        uBM.Username = 'test1234fdfdfefe4trfd34BM@test.com';
        uBM.CommunityNickname = 'test122BM';
        uBM.LocaleSidKey = 'en_US';
        uBM.ProfileId = profileId.Id;
        uBM.TimeZoneSidKey = 'GMT';
        uBM.LanguageLocaleKey = 'en_US';
        uBM.EmailEncodingKey = 'UTF-8';
        insert uBM;
        
        User uITM = new user();
        uITM.LastName = 'Test ITM';
        uITM.Email = 'testITM@test.com';
        uITM.Alias = 'ITM';
        uITM.Username = 'test1234fdfdfefe4trfd34ITM@test.com';
        uITM.CommunityNickname = 'test122ITM';
        uITM.LocaleSidKey = 'en_US';
        uITM.ProfileId = profileId.Id;
        uITM.TimeZoneSidKey = 'GMT';
        uITM.LanguageLocaleKey = 'en_US';
        uITM.EmailEncodingKey = 'UTF-8';
        insert uITM;

        User uDL = new user();
        uDL.LastName = 'Test DL';
        uDL.Email = 'testDL@test.com';
        uDL.Alias = 'DL';
        uDL.Username = 'test1234fdfdfefe4trfd34DL@test.com';
        uDL.CommunityNickname = 'test122DL';
        uDL.LocaleSidKey = 'en_US';
        uDL.ProfileId = profileId.Id;
        uDL.TimeZoneSidKey = 'GMT';
        uDL.LanguageLocaleKey = 'en_US';
        uDL.EmailEncodingKey = 'UTF-8';
        insert uDL;
        
        User uITDL = new user();
        uITDL.LastName = 'Test ITDL';
        uITDL.Email = 'testITDL@test.com';
        uITDL.Alias = 'ITDL';
        uITDL.Username = 'test1234fdfdfefe4trfd34ITDL@test.com';
        uITDL.CommunityNickname = 'test122ITDL';
        uITDL.LocaleSidKey = 'en_US';
        uITDL.ProfileId = profileId.Id;
        uITDL.TimeZoneSidKey = 'GMT';
        uITDL.LanguageLocaleKey = 'en_US';
        uITDL.EmailEncodingKey = 'UTF-8';
        insert uITDL;
        
        DOUM_Terms_Conditions__c ter = new DOUM_Terms_Conditions__c();
        ter.DOUM_TCs_A_D__c = date.Today();
        ter.DOUM_TCsContent__c = 'New Terms of Conditions';
        ter.DOUM_TCs_Approval_Status__c = 'Approved';
        ter.DOUM_TCs_RD__c = NULL;
        insert ter;
        
        DOUM_Requester__c req = new DOUM_Requester__c();
        req.Name = 'Apex Tester Requester';
        req.DOUM_MoC__c = uBM.Id;
        insert req;
        
        DOUM_DOU__c DOU = new DOUM_DOU__c();
        DOU.DOUM_Requester__c = req.Id;
        DOU.DOUM_Domain__c = 'Activity';
        DOU.DOUM_DOUVersion__c = 1;
        DOU.DOUM_DOU_Approval_Status__c = 'In Approval';
        DOU.DOUM_Req_BMgr__c = uBM.Id;
        DOU.DOUM_Requester_IT_Mgr__c = uITM.Id;
        DOU.DOUM_Domain_Lead__c = uDL.Id;
        DOU.DOUM_IT_Domain_Lead__c = uITDL.Id;
        DOU.DOUM_BM_Approval__c = FALSE;
        DOU.DOUM_ITM_Approval__c = FALSE;
        DOU.DOUM_DL_Approval__c = FALSE;
        DOU.DOUM_ITDL_Approval__c = FALSE;
        DOU.DOUM_WAF__c = NULL;
        DOU.CreatedDate = System.now() - 10;
        DOU.LastModifiedDate = System.Now()- 5;
        insert DOU;
            
        DOUM_DOU__c DOU2 = new DOUM_DOU__c();
        DOU2.DOUM_Requester__c = req.Id;
        DOU2.DOUM_Domain__c = 'Activity';
        DOU2.DOUM_DOUVersion__c = 2;
        DOU2.DOUM_DOU_Approval_Status__c = 'In Approval';
        DOU2.DOUM_Req_BMgr__c = uBM.Id;
        DOU2.DOUM_Requester_IT_Mgr__c = uITM.Id;
        DOU2.DOUM_Domain_Lead__c = uDL.Id;
        DOU2.DOUM_IT_Domain_Lead__c = uITDL.Id;
        DOU2.DOUM_BM_Approval__c = FALSE;
        DOU2.DOUM_WAF__c = NULL;
        DOU2.DOUM_ITM_Approval__c = FALSE;
        DOU2.DOUM_DL_Approval__c = FALSE;
        DOU2.DOUM_ITDL_Approval__c = FALSE;
        insert DOU2;
        
		DOUM_DOU__c DOU3 = new DOUM_DOU__c();
        DOU3.DOUM_Requester__c = req.Id;
        DOU3.DOUM_Domain__c = 'Alignment';
        DOU3.DOUM_DOUVersion__c = 1;
        DOU3.DOUM_DOU_Approval_Status__c = 'In Approval';
        DOU3.DOUM_Req_BMgr__c = uBM.Id;
        DOU3.DOUM_Requester_IT_Mgr__c = uITM.Id;
        DOU3.DOUM_Domain_Lead__c = uDL.Id;
        DOU3.DOUM_IT_Domain_Lead__c = uITDL.Id;
        DOU3.DOUM_BM_Approval__c = TRUE;
        DOU3.DOUM_ITM_Approval__c = TRUE;
        DOU3.DOUM_DL_Approval__c = FALSE;
        DOU3.DOUM_ITDL_Approval__c = FALSE;
        DOU3.DOUM_WAF__c = NULL;
        DOU3.CreatedDate = System.now() - 10;
        DOU3.LastModifiedDate = System.Now()- 3;
        insert DOU3;

		DOUM_DOU__c DOU4 = new DOUM_DOU__c();
        DOU4.DOUM_Requester__c = req.Id;
        DOU4.DOUM_Domain__c = 'Customer';
        DOU4.DOUM_DOUVersion__c = 1;
        DOU4.DOUM_DOU_Approval_Status__c = 'In Draft';
        DOU4.DOUM_Req_BMgr__c = uBM.Id;
        DOU4.DOUM_Requester_IT_Mgr__c = uITM.Id;
        DOU4.DOUM_Domain_Lead__c = uDL.Id;
        DOU4.DOUM_IT_Domain_Lead__c = uITDL.Id;
        DOU4.DOUM_BM_Approval__c = TRUE;
        DOU4.DOUM_ITM_Approval__c = TRUE;
        DOU4.DOUM_DL_Approval__c = FALSE;
        DOU4.DOUM_ITDL_Approval__c = FALSE;
        DOU4.DOUM_WAF__c = NULL;
        DOU4.CreatedDate = System.now() - 10;
        DOU4.LastModifiedDate = System.Now()- 3;
        insert DOU4;
		
        system.runas(u){
                        
            Test.startTest();
            
            DOUM_WaitingOnApprovals.ListDOUs();
            
            system.debug('DOU'+DOU);
            system.debug('DOU2'+DOU2);
            system.debug('DOU3'+DOU3);
            system.debug('DOU4'+DOU4);
            
                                  
            system.debug('DOU'+DOU.DOUM_WAF__c);
            system.debug('DOU2'+DOU2.DOUM_WAF__c);
            system.debug('DOU3'+DOU3.DOUM_WAF__c);
            system.debug('DOU4'+DOU4.DOUM_WAF__c);
            
            
            system.assert(DOU.DOUM_WAF__c == NULL);
            system.assert(DOU2.DOUM_WAF__c == NULL);
            system.assert(DOU3.DOUM_WAF__c == uDL.Email);
            system.assert(DOU4.DOUM_WAF__c == NULL);
            
            Test.stopTest();
        }        

	}
}

 In the debug list the debug in the class for the email, waf, and "after update Waf" all have the correct value but in the test class the system.debug('DOU'+DOU.DOUM_WAF__c), system.debug('DOU2'+DOU2.DOUM_WAF__c), system.debug('DOU3'+DOU3.DOUM_WAF__c), system.debug('DOU4'+DOU4.DOUM_WAF__c) are all null. I cant seem to find why the values get set to null after the update works.