Querying All Records with a SOQL Statement #inSalesforce

SOQL statements can use the ALL ROWS keywords to query all records in an organization, including deleted records and archived activities.


Example 1:

If you try SELECT Id, Name FROM Candidate__c ALL ROWS it results in MALFORMED_QUERY: ALL ROWS not allowed in this context. or Unknown error parsing query.

Solution:

     1. Developer Console | Debug menu | Open Execute Anonymous Window menu

     2. In Enter Apex Code, enter the following and Check the 'Open Log' checkbox. 

List<Candidate__c> candidates = [SELECT Id, Name FROM Candidate__c ALL ROWS];

System.debug(JSON.serialize(candidates));

     3. Click 'Execute' to run the code and check the log file for the result.


Visualhttps://www.youtube.com/watch?v=_-VucJuGXB0


Example 2:

System.assertEquals(2, [SELECT COUNT() FROM Contact WHERE AccountId = a.Id ALL ROWS]);


Note: You cannot use the ALL ROWS keywords with the FOR UPDATE keywords.

Comments