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
El.HEl.H 

Log shows different result to test assertion

Hello,

I am trying to write a simple test for a trigger:
trigger UpdateHasRelatedCase on Case (after insert, after update) {
    
   List<Contact> toUpdate = new List<Contact>();
    for(Case ca: Trigger.new)
    {
        Boolean closed = ca.closedDate!=null;
        Boolean openCases=false;
        Contact con = [select id,firstname, has_open_case__c from Contact where id =:ca.contactId limit 1];
        
        if(!closed)
        {
            
            if(con!=null)
            {
               
                con.has_open_case__c=true;
            }
                
        }
        else
        {
            list<Case> cases = [select contactId, casenumber,ClosedDate from Case where contactId=:con.id];
            for(Case c: cases)
            {
                if(c.closedDate==null)
                {
                    if(con!=null)
                    	openCases=true;
                    break;
                }
            }
            
            con.has_open_case__c= openCases;  
        }
        toUpdate.add(con);
    }
    update toUpdate;

}

basically it will just check if the current context (case) is open and set a the has_open_case__c field on Contact to true.  If it is not open, it will check all other related Cases and if it finds any that are open, it will set the field to true.
if none of the above occurs then the field is false.

I have written system.debug statements in different places in this trigger, and found that I get the expected results. However, in my test class :
@isTest
public class TestUpdateHasRelatedCase {
    
    @isTest
    static void TestHasRelated()
    {
         Contact cli=new Contact
           (
               firstname='TestCli',
               lastname='test',
               Relationship_Status__c='Prefer not to say',
               Gender__c='Prefer not to say',
               leadSource='Friend',
               mailingstreet='123 SomeTown',
               contact_ethnicity__c='Prefer not to say',
               mailingcity='SomeCity,
               mailingpostalcode='CCC CCC'
           );
        insert cli;
        
        Case ca = new Case
        (
            contactid=cli.id,
            ethnicity__c = 'Other Ethnic Group',
            delegate__c='0050O000008KiQzQAK',
            subject='test',
            origin='Red Cross',
            Reason='New problem',
            recordtypeid='0123Y000000dbAUQAY',
            Status='Closed'
              
        );
        insert ca;
        
        System.assertEquals(true,cli.has_open_case__c);
    }
    

}
the result of has_open_case__c is not the same value as when I printed it inside the trigger, and leading to failure of the test.

Any help on why this is happening would be appreciated !

Thank you ,
El

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Here from your code closed variable is returning as True and for the case record it is Closed status so we get ClosedDate for this record So the has_open_case__c is returning as False which is expected.

I hope your assertion should be True .

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
El.HEl.H
Hello,

Thank you for the response, when I have logged the variables inside the trigger I get the following results:

User-added image

The trigger has coverage on the line it is assigning the has_open_case field to true, and after update statement, you can see above that the record has changed to true.  The problem is that the tests, and any records I make in SF through the interface still return false in this case.  Same with opposite scenario.

I can make cases through the interface, update them to closed and still have the records in the sandbox reporting as has_open_case=true

I am really confused what is causing this since debug statements inside the trigger itself report perfectly that it updated as I wanted it to (when i check logs after the test run), but those changes then do not actually apply to the database and the tests do not pick them up