Magic Parents

So I wrote a piece of code that should by all accounts not work (but it's kinda cool, it returns the top level account ids via hierarchy for any accounts passed to save on queries in does this 4 levels at a time):
On code review by our own Chuck Lidell, he pointed out, there is a potential of a null error being thrown if a parent is null when line "targetedAccount.Parent.Parent.Parent.ParentID != null" is hit. Sure I wrote the code without my coffee, but it passed testing... so we dug deeper. This got more interesting
this doesn't throw any errors, even though the depth is not physically possible in a query.
Change the query to: and you hit "System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Parent
AnonymousBlock: line 16, column 1"
Amend query to: And again no errors are thrown on the whole debug block.
Works for other references too
No errors thrown
Conclusion, past a depth of 2, as long as the schema is valid, there is no check for presence of field in query.


Popular posts from this blog

Why Running 237km in Sahara and Being a Salesforce Dev is Exactly the Same

A Recursive Headache: Custom Security Solution