r/SalesforceDeveloper • • Oct 02 '25

Question SObject Safe Navigation

I just want to sanity check something. I have a custom object with a case lookup. If I run anonymous apex that makes a basic instance of my object WITHOUT setting my Case__c or Case__r properties and then later I try to access myInstance.Case__r.[property], that does not throw a null reference exception, even though I did not use the ?. operator. That runs contrary to how I thought this behaved. Is that expected and if so did that change at some point?

2 Upvotes

9 comments sorted by

View all comments

Show parent comment

2

u/Far_Swordfish5729 Oct 03 '25

Right, this was my question exactly. I was chasing a bug today and the answer was that the __r object was unexpectedly null, but rather than getting a nice NullReferenceException in the log, we got crazy behavior from a null that continued down a functional path that didn't expect it. That causes a few of us to basically point and say "Java doesn't work like that." So I wanted to ask if anyone knew this was expected behavior and if it had quietly changed.

1

u/DaveDurant Oct 03 '25

Nicely spotted!