I am apparently missing something in the creation of my unit test data. I would like to create both an Account and an Open Activity (task) for the account.

I am able to successfully insert both records, and I receive results when querying the Account and Task individually. My issue is when I attempt to find the Open Activities for the Account.

Account Creation Code

Account a = new Account(Name='Sample Account');
insert a;

Task Creation Code

List<Task> tasks = new List<Task>();
tasks.add(new Task(
    ActivityDate = Date.today().addDays(7),
    Subject='Sample Task',
    WhatId = a.Id,
    OwnerId = UserInfo.getUserId(),
    Status='In Progress'));

insert tasks;

After the records are created, this query returns results:

Id aId = a.Id;
List<Task> taskRecords = [Select Id, WhatId, Subject from Task where WhatId = :aId];

While this does not:

List<Account> accountWithOpenActivities = [Select Name, (Select Id, WhatId, Subject From OpenActivities) from Account where Id = :aId];
  • Once activities are closed or completed then i think they dont recide in open activities?right? Commented Jul 9, 2013 at 20:07
  • That sounds correct. My task status is set to "In Progress" and should not be closed, which is why I was expecting results from the OpenActivities query. Commented Jul 9, 2013 at 20:10
  • From your question i see you have said status as "Completed".may be Typo :) Commented Jul 9, 2013 at 20:12
  • @MohithKumar That is the configuration that defines a completed task. I have removed that line from the question so as to not add irrelevant details. Commented Jul 9, 2013 at 20:22
  • 2
    I actually ran this same code and for me both your queries returned results. You might want to look for any other workflow/trigger in you org that are causing the task to get closed/completed.
    – Sai
    Commented Jul 9, 2013 at 20:25

2 Answers 2


Any chance your running user in the testmethod falls afoul of these restrictions (from SFDC Doc)

In order to prevent performance issues while still providing the related list functionality, there are some restrictions on users who do not have “View All Data” permission. Such users must comply with the following restrictions:

In the main clause of the relationship query, you can reference only one record. For example, you cannot filter on all records where the account name starts with A, but must reference a single account record.

You cannot use WHERE clauses.

You must specify a limit to the number of rows returned, less than 500.

You must sort on ActivityDate and LastModifiedDate, descending order: ORDER BY ActivityDate DESC, LastModifiedDate DESC

  • Thanks! Adding the SeeAllData=true annotation did the trick, which I didn't expect I would need to do since the same user had just created the tasks. Commented Jul 10, 2013 at 19:27

Try doing the subquery on Task

[Select Name, (Select Id, WhatId, Subject From Task) from Account where Id = :aId];

You mention something about the closed task status value being set to "Completed". Once a Task is "Completed", it's no longer an "Open Activity". So, perhaps you want to add Status to your query on Tasks? Its not clear to me at what point you're having the problem in your test class to be able to advise further.

  • I have removed that line. In the code I supplied, my Status is set to "In Progress". The code works as expected in the pages, my issue exists when attempting to add a record for my unit testing. Commented Jul 9, 2013 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .