+ Start a Discussion

Interesting SOQL Puzzle - when do Deleted records count?



I've been working through some SOQL queries, as I'm trying to determine how many open Tasks are attached to our cases.


So I've got a SOQL query like this as part of a batchable class:


Select l.Id, l.Open_Tasks__c, l.CaseNumber, (Select Id, WhatID, IsClosed From Tasks t where t.IsClosed = False and t.IsDeleted = False) From Case l


In this case, if I don't filter out the deleted tasks, it is being returned in the result.


I've got another variation where I don't seem to need to include the filter, where I've built a set of case IDs at the beginning:


Select l.Id, l.Open_Tasks__c,
            (Select Id From Tasks where IsClosed = False)
            From Case l where Id in :CaseIDs


[This logic is done in an Apex trigger]


I'm not sure what I'm missing something here -- and I'm getting the results that I expect now if I include the IsDeleted filter (in both cases) -- but I'm wondering what causes the difference in the results as I find it a bit odd.