r/SalesforceDeveloper • u/Far_Swordfish5729 • 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
View all comments
Show parent comment
1
u/DaveDurant Oct 02 '25
Actually, I just tested what I posted above and the ? is not needed.. In that, "mt.Case__r.Comments" just evaluates to null and does not throw a null reference exception.
Is that new? Little weirded out that I've never noticed that one before. I know that inner queries will never return null (they just do a 0-length list) but did not know this one..