function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DaveFFDaveFF 

TaskRelation vs TaskWhoRelation during unit tests

I am writing (and testing) a Batch Apex job that starts off by looking up Contacts who have a particular type of Task. Each such Task should only contain one Who (a Contact) and one What (an Account), but I want the code to be future-proof. I am at this stage only interested in the IDs of the Contacts, so I tried querying the TaskWhoRelation, and was surprised to find it empty. Adding extra assertions to the test reveals inconsistent behaviour between TaskRelation and TaskWhoRelation.
    System.assertEquals(1, [
      SELECT COUNT()
        FROM Task
       WHERE Activity_Type__c = :SurveyOrgBatch.ACTYPE_WAYN
    ]);
    System.assertEquals(1, [
      SELECT COUNT()
        FROM TaskRelation
       WHERE Task.Activity_Type__c = :SurveyOrgBatch.ACTYPE_WAYN
         AND IsWhat = false
    ]);
    System.assertEquals(1, [
      SELECT COUNT()
        FROM TaskWhoRelation
       WHERE Task.Activity_Type__c = :SurveyOrgBatch.ACTYPE_WAYN
    ]);

I insert a single Task with the WhoId and WhatId set to appropriate test records. The first two assertions pass. The third fails with "System.AssertException: Assertion Failed: Expected: 1, Actual: 0"

I guess I can just use TaskRelation instead, but can TaskWhoRelation be used in batch jobs? During testing? In things that are tested?