I have a logic whenever Task is created/updated if the task owner is Active. for that I have just written sample class to test it. I am getting the owner is always inactive, even though running is user is active in the system. Why the owner is coming as Inactive?

Sample Class:

public class TaskSampleTrigger {
    public static void createTrialOpportunity(Task record){
       Task rec=[SELECT id,owner.isActive FROM Task where id=:record.Id LIMIT 1];
    Boolean validTouch=isValidTouch( record);
    System.debug('******Recor****** '+rec);
    System.debug('******Queried User is Active****** '+rec.owner.isActive);
    System.debug('******User is Active****** '+validTouch);
    public static Boolean isValidOwner(Task record){
        return record.owner.isActive;

Test Class:

private class TaskSampleTriggerTest {
    static testMethod void createSampleTest(){
        Lead newLead = TestingUtils.createLeads(1, 'Sample')[0];
        newLead.Email = '[email protected]';
        newLead.Country='United States';
        insert newLead;     
        Account account = TestingUtils.createAccounts(1, new String[]{'TEST'})[0]; 
        account.BillingCountry ='United States';
        insert account;
        Task[] tasks = new Task[0];
        tasks.add(new Task(WhoId = newLead.Id, Lead__c=newLead.Id, Priority='High',Status='Completed',Status_Detail__c ='1',Subject='AATest',Type='Call'));
        insert tasks;         

Result: enter image description here

enter image description here

2 Answers 2


Relationships are not automatically populated in trigger context, so, presumably, you're trying to do something like:

for(Task record: Trigger.new) {

Owner is not populated, so Owner.IsActive returns false (the default value for this field).

  • yes, I get it, but I am trying to explicitly querying the record owner, but why is it still returning false? queried field is also returning false. am I missing anything?
    – Ajay
    May 29, 2023 at 13:40

Test classes are not run in the context of user executing them ...

Use System.runAs method to mimic a user and make sure for to define isActive true for this user and insert records with this user as owner


  • 1
    Running tests as a particular user could be helpful in some contexts. However, this is not the issue in this particular case (the issue appears to be attempting to access related data from a record from a trigger context variable).
    – Derek F
    May 29, 2023 at 1:50

You must log in to answer this question.

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