function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Batch Apex SOQL includes Deleted Records?

Can someone confirm if this is expected behavior...?


I have a batch apex class that calculates a Contact's opportunity total and updates the contact record. It works fine, except I am seeing an odd error. This is an example of the SOQL I use, where ctotal is the instance of the Batch Apex Class.



ctotal.query = 'Select Id, total__c,(Select OpportunityId, Opportunity.StageName, Opportunity.Date_Received__c, Opportunity.Amount from OpportunityContactRoles where Opportunity.StageName = \'Collected\' Order by Opportunity.CloseDate ASC) From Contact WHERE Id = \'0034000000U842kAAB\'';



The contact I am filtering for has one Opportunity that matches the SOQL criteria, and if I submit this SOQL via the system log, it correctly returns on record.


But when I execute this in the Batch Context, it is returning two rows. Looking into it further, I discovered the second record is a deleted record, which only appears if I use the ALL ROWS parameter in the system log.


Do I need to filter my batch apex to disregard deleted rows, or should I be setting some other paramter, or is this a bug?







I used to face this issue as well. My solution is to filter by using isDeleted=false.




Just to confirm for anyone arriving here - SF confirmed it was an issue, and you should filter by IsDeleted = FALSE...


I'll update the post if I hear more about a fix...


Just as a clarifying note, this IsDeleted bug only applies to subselects in batches.


I.E. in the above example, if the contact had been deleted, it would not be returned.

Scott GraboScott Grabo

A further clarification: it's not just in subselects. See my post.