0

I have an apex class that I am testing that queries for the record Owner information on leads. When I try to access the Owner information the values are null. However, when I query the user object directly the values are populated. This only happens in test execution

Apex:

//get leads
Map<Id, Lead> leads = new Map<Id, Lead>([
    SELECT Id, Email, Owner.Email, OwnerId
    FROM Lead
    WHERE Id IN :leadIds
]);

//Debug email via lookup
System.debug('lead owner email: ' + leads.values().get(0).Owner.Email);
//debug email via direct query
System.debug('User Email: ' + [SELECT Email FROM User WHERE Id = :leads.values().get(0).OwnerId].Email);

Debugs:

15:40:42.605 (9825005646)|USER_DEBUG|[148]|DEBUG|lead owner email: null
15:40:42.605 (9825315527)|SOQL_EXECUTE_BEGIN|[149]|Aggregations:0|SELECT Email FROM User WHERE Id = :tmpVar1
15:40:42.605 (9829377675)|SOQL_EXECUTE_END|[149]|Rows:1
15:40:42.605 (9829607692)|USER_DEBUG|[149]|DEBUG|User Email: Redacted but is a valid email
  • Is the get leads your test or your code? Are you sure you created the lead in the test? – Dan Wooding Nov 25 '19 at 22:00
  • It is code being tested, and yes they are populated otherwise I would be getting an IndexOutOfBounds error accessing them in the debugs. – TemporaryFix Nov 25 '19 at 22:02
2

I think the reason you're getting this is because the lead owner field is polymorphic. The lead owner can be a user, queue, partner user, etc.

So, as a result, it's easier to run 2 seperate queries or do a join on the user object.

Here's some docs on polymorphic fields - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_polymorphic_relationships.htm

  • That is a good theory – TemporaryFix Nov 25 '19 at 22:12
  • I updated my post @TemporaryFix to include documentation on polymorphic keys... i think that'll do it for you. – PartOfTheOhana Nov 25 '19 at 22:14
-1

Your lead is owned by a queue not a user. PartOfTheOhana is right in that it's polymorphic but if the owner was a user your debug would work.

Example:

@IsTest
static void methodName()
{
    Profile profId = [Select Id FROM Profile WHERE Name = 'NPD Standard'];
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;

    User tuser = new User(
        IsActive = true,
        FirstName = 'Testing',
        LastName = 'User',
        Email = uniqueName + '@dan' + orgId + '.com',
        Username = uniqueName + '@dan' + orgId + '.com',
        EmailEncodingKey = 'UTF-8',
        Alias = uniqueName.substring(18, 23),
        TimeZoneSidKey = 'America/Los_Angeles',
        LocaleSidKey = 'en_US',
        LanguageLocaleKey = 'en_US',
        ProfileId = profId.Id);
    insert tuser;

    Lead l = new Lead();
        l.OwnerId = userinfo.getUserId();
        l.LastName = 'Dan';
        l.Email = 'dan@dan.com';
        l.Company = 'Dan';
        l.OwnerId = tuser.Id;
    insert l;   

    Set<Id> leadIds = new Set<Id>();
    leadIds.add(l.Id);

    Map<Id, Lead> leads = new Map<Id, Lead>([
        SELECT Id, Email, Owner.Email, OwnerId
        FROM Lead
        WHERE Id IN :leadIds
    ]);

    System.debug('lead owner email:: ' + leads.values().get(0).Owner.Email);   

|DEBUG|lead owner email:: 00d0r0000000gnfeai20191125171821456832@dan00d0r0000000gnfeai.com

If you switch the owner to a queue you get the same results:

@IsTest
static void methodName()
{
    Group q = [select Id from Group where Type = 'Queue' LIMIT 1];

    Lead l = new Lead();
        l.OwnerId = userinfo.getUserId();
        l.LastName = 'Dan';
        l.Email = 'dan@dan.com';
        l.Company = 'Dan';
        l.OwnerId = q.Id;
    insert l;   

    Set<Id> leadIds = new Set<Id>();
    leadIds.add(l.Id);

    Map<Id, Lead> leads = new Map<Id, Lead>([
        SELECT Id, Email, Owner.Email, OwnerId
        FROM Lead
        WHERE Id IN :leadIds
    ]);

    System.debug('lead owner email:: ' + leads.values().get(0).Owner.Email); 

|DEBUG|lead owner email:: null

  • It's not owned by a queue, I am querying the user with the same exact Id and getting the result. Again, if that were not the case, the debug would fail. – TemporaryFix Nov 25 '19 at 23:37

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.