Not all tasks returned in a SOQL query

I have a SOQL query in my Apex Controller that queries tasks according to some conditions:
List<Task> tasks = [SELECT Id FROM Task WHERE Subject = 'Some Subject'];

However, the query does not return the correct number of rows - some are missing. When I run the same query with RealForce explorer, the correct number of tasks is returned.
graham on 16-05-2013 08:29
The reason might be that tasks older than a year are automatically archived by Salesforce and won't be returned in normal queries. To query for all tasks, use the ALL ROWS keyword at the end of your SOQL query:
List<Task> tasks = [SELECT Id FROM Task WHERE Subject = 'Some Subject' ALL ROWS];

The use of ALL ROWS will result in all tasks being returned, including archived and deleted tasks. Now, you usually don't want deleted tasks to be returned, so you need to restrict this:
List<Task> tasks = [SELECT Id FROM Task WHERE Subject = 'Some Subject' AND IsDeleted = false ALL ROWS];
graham on 16-05-2013 08:31

Post an answer

Do you have a solution to this problem, or want to comment on it? Other users will appreciate your help!
Your answer or comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems