I have a query on accounts with an inner query on ActivityHistory

string query;
query =' SELECT id,ownerid,o,';
query+='  (SELECT id,AccountId, WhatId,status,createdDate,activityDate FROM ActivityHistories)';
query += ' FROM account;

Then I have a for loop for the activity history:

for(ActivityHistory acctHist : historiesList){ //some stuff happens here}

When I write my tests creating An account/ contact and task which are all associated I can't get the test to enter that loop?

Here is the test:

    account a1 = new account();
    a1.name = 'Acct Test 1' + system.now()+1;


    system.debug('***a1***' + a1);

    contact c1 = new contact();
    c1.AccountId = a1.Id;
    c1.FirstName = 'C1 First Name';
    c1.LastName = 'C1 Last Name';

    system.debug('***C1***' + c1);

    task t1 = new task();
    t1.whatId = a1.id;
    t1.whoId = c1.id;
    t1.Type = 'Call - Outbound';
    t1.status = 'Completed';
    t1.activityDate = system.today();
    t1.calltype = 'Outbound';
    insert t1; 
Test.StartTest(); CallClass = new CallClass(); Test.StopTest();

All of the system debugs show the relationships exist but the loop is still not entered.

  • 1
    The test data doesnot create record in history table .The history record is created only once data is commited to database and data not commited in database in test class.I guess you can insert a history record in the table to improve code coverage. Oct 12, 2012 at 18:27
  • I attempted to create ActivityHistory record but the attributes are not writeable. Oct 12, 2012 at 18:36
  • Ok so finally may be we need to query from database to increase code coverage Oct 12, 2012 at 18:38

3 Answers 3


To get around this I have removed the inner query and added an additional one where the list the query populates mirrors the functionality of ActivityHistories.

Thanks for the help/direction/research.


That does seem odd, I tried executing that just anonymous and it seemed to return a result okay. However just reading about ActivityHistory, it might be an idea to add a ORDER BY and LIMIT Clause to the query. (http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_activityhistory.htm) This might be particularly relevant especially if you are executing the Test as a User with no View All Data permission.


SELECT id,AccountId, WhatId,status,createdDate,activityDate FROM ActivityHistories ORDER BY ActivityDate DESC, LastModifiedDate DESC LIMIT 10

As an aside, I got

System.assertEquals(1, ((Account)Database.query('Select Id, (Select Id, Status from ActivityHistories) from Account WHERE Id = \'' + acc.Id + '\'')).ActivityHistories.size());

to work

  • Since the data is not commited history table records wont be formed .May be you would like to experiment this in test class .Execute anonymous data gets commited Oct 12, 2012 at 20:08
  • ActivityHistory is auto created when an Activity is marked 'Completed'. You CANNOT create an ActivityHistory Record explicitly, it is Read Only. Oct 12, 2012 at 20:11
  • Are you sure that this is created in test class to after we Mark activity completed? Oct 12, 2012 at 20:15
  • Yes. I've tested this several times. Have you ? Oct 12, 2012 at 20:23
  • Yes i too did in my test class and same problem as jonatham mentions .Activity history table record not created if i query back Oct 12, 2012 at 20:25

How about instead of querying for ActivityHistories, you query for (Events where StartDateTime < :system.now()) and (Tasks where IsClosed = TRUE). Those can be created in a test class, and it seems like you could get the same information that way.

  • That is exactly what I ended up doing. Basically built a query that returned the same results as ActivityHistories. Oct 13, 2012 at 22:19

You must log in to answer this question.

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