I can query the child cases for current case by something like below,
list<case> parentids=trigger.new
select id,status from case where parentid=:parentids
What if the children have more children? Does it make sense to have more than one level hierarchical relationship? Should I use lookup filter on parent case to allow only parent cases linked to child case and not child to child relationship? What's the best practice?
Is that possible to query all cases related to parent case (child, grand child etc)? Can somebody give me an idea to retrieve all related records?
Best Answer
The basic idea is to create a formula for
Ultimate Parent
Then, you can get your relevant cases as follows:
You might be able to use
recalculateFormulas()
to get the values without doing an inner-join-sub-select, but there is a known issue dealing with cross-object logic, so I have my doubts it would work.Update
The
recalculateFormulas
method does give you the correct value, but also causes an exception if you execute it on the trigger records: