2

I have the following test setup method:

@TestSetup
public static void testSetup() {
    //system.runAs(u) {
        //JITTriggerManagement.toggleTrigger('Lead');

        Lead l = new Lead(
            FirstName = 'F',
            LastName = 'L',
            Company = 'C',
            Status = '100-New/Open',
            Email = 'test@example.com',
            OwnerId = UserInfo.getUserId()
        );

        insert l;

        for (lead lz : [
            SELECT Id, Reassign__c, LeadTerritory__c, 
                OwnerId, Owner.Name, Owner.IsActive,
                CreatedById, CreatedBy.Name, CreatedBy.IsActive
            FROM Lead
        ]) {
            system.debug(json.serialize(lz.owner));
            system.debug(lz.Reassign__c + ':' 
                + lz.OwnerId + ':' + lz.Owner.Name + ':' + lz.Owner.IsActive + ':' 
                + lz.CreatedById + ':' + lz.createdBy.Name + ':' + lz.CreatedBy.IsActive);
        }
    //}
}

I am creating a lead and then querying for both the owner and the created user. The debug shows that the OwnerId has a value but the owner object is completely null. The CreatedById is the exact same as the owner Id, but the created by user object is populated correctly:

false:

005f4000003VXFwAAO:null:false:

005f4000003VXFwAAO:Admin User:true

This is to test a batch class. The batch class start method is belwo:

global Database.queryLocator start(Database.BatchableContext ctx) {
    DateTime fifteenMinutesAgo = test.isRunningTest() ? 
        system.now().addMinutes(15) : system.now().addMinutes(-15);

    return Database.getQueryLocator([
        SELECT Id, LeadTerritory__c, Reassign__c, CreatedBy.Name, Owner.Name, Owner.IsActive
        FROM Lead 
        WHERE Reassign__c = true
            AND CreatedDate < :fifteenMinutesAgo
    ]);
}

Here the important part is the "reassign__c" field which is a formula based on owner and it is showing false when it should not be. I am attempting to query the owner record to verify the formula.

The reason I added createdBy to the query was because the createdBy and Owner are the same user. This means I can verify the user fields for the purposes of diagnosing issues with the formula. The information I pulled from the created by field show that the formula should be true, but it is not.

The formula doesn't seem to be working, and my only current theory is that it is because it cannot access the owner's information.

Update

Attempted the following in execute anonymous window:

Lead l = new Lead(
    FirstName = 'F',
    LastName = 'L',
    Company = 'C',
    Status = '100-New/Open',
    Email = '1234567890@example.com',
    OwnerId = UserInfo.getUserId()
);

insert l;

for (lead lz : [
    SELECT Id,
    OwnerId, Owner.Name, Owner.IsActive, Owner.Type,
    CreatedById, CreatedBy.Name, CreatedBy.IsActive
    FROM Lead
    WHERE Email = '1234567890@example.com'
]) {
    system.debug(lz.owner.type + ':' + lz.Owner.Name + ':' + lz.Owner.IsActive);
}

The code worked exactly as expected. Copying this exact code back into the test class resulted in the same behavior where owner was not accessible.

  • 1
    This seems a bug to me. – Jayant Das Apr 4 at 16:15
1

Unfortunately my only conclusion at this point is that it is a bug in the testing framework.

When I remove @isTest and @testSetup annotations from the code, it works perfectly fine. (system.runAs is test specific as well but I tested without system.runAs while keeping the annotations and the behavior continued; suggesting that it is not the issue).

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.