I need to test an archived task in my test method. According to Salesforce it will be archived if it is closed and activity date is older than 365 days. Yet this does not work:

static testMethod void shouldRollupArchivedRecords() {
    Account a = new Account(Name='Sample Account');
    insert a;

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

    insert tasks;

    tasks = [SELECT Id, isArchived, ActivityDate, IsClosed FROM Task];
    tasks = [SELECT Id, isArchived, ActivityDate, IsClosed FROM Task ALL ROWS];

IsArchived is false when I perform the query after inserting. How do I make it archived (it is not a writable field so I can't simply do isArchived = true)? Also if I create a task in the user interface with status = completed and due date 2 years ago it still is not archived. Any ideas?


The archiving process occurs automatically on a background task provided by the system (I think every hour or so). You can't actually force the IsArchived flag to be set to true. You will not be able to test code that attempts to create a task or event that is archived, because the flag simply will not set. This is one of the rare situations where you need to "fake" out your test by providing some hook into the code so you can find these "archived" test tasks.

| improve this answer | |
  • I was afraid of that. I created a completed task with a due date over of over 1 year ago a couple hours ago and it still in not archived. If that ever gets archived then I could just do seeAllData=true. Any other possible solutions? – Joseph Bauer Mar 31 '15 at 23:07
  • I know this is a super old q&a but I just created an Idea to give us the ability to archive activities manually & through Apex. Everybody that wants this functionality should upvote this: success.salesforce.com/ideaView?id=0873A000000lKpVQAU – Robert Watson Feb 21 '18 at 21:21
  • @RobertWatson It'd be nice if they simply had a method for test purposes only that allowed you to bypass any and all system checks (e.g. allow us to set IsArchived directly, or specify nonsensical user ID values, etc). Still, I like the Idea; you have my vote. – sfdcfox Feb 21 '18 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.