I am working with Community users and I need to query both their User and Contact details at the same time.

When I am performing a relationship query like

[Select Id, Email, ContactId, Contact.FirstName, Contact.LastName From User where...]

I am getting the correct field values from User, but the fields from Contact have the value null.

If I query these objects separately, I am able to see the values from the Contact object's fields. I am system admin and I have access on all fields specified in the SOQL query. The OWD for Contact is Controlled by Parent and for User is private.

I could do the queries separately in my code but I wonder what is the cause why the relationship query is giving such a result?

  • 2
    I'm having no issues using the same query and getting the contact info. Are you filtering on users that have a ContactId? May 19, 2020 at 11:51
  • @KrisGoncalves I am filtering for users which have a Community profile, therefore all of them will have ContactId. Also, I am in a test class and the only users I have there have a ContactId for sure. And besides I can see in my logs that ContactId exists but the fields on that Contact are not visible for some reason. May 19, 2020 at 11:54
  • It might be more related to your test class if you can include any code snippets from it. You can confirm that the query works outside of the test context first to narrow it down. May 19, 2020 at 12:39
  • @KrisGoncalves unfortunately the code is work-related so I cannot explicitly share it. I just insert a contact an a user related to it. Of course I set the first name, last and so on on the contact. I have also tried to do this kind of query in anonymous window on effective data and the same issue happens. Split queries work, but relationship query does not. May 19, 2020 at 12:50
  • Can you just share the User creation code and the SOQL WHERE criteria? May 19, 2020 at 13:30

1 Answer 1


I have investigated in more details and apparently, if you perform a relationship query and then display the result using System.debug() method, it will not display the related object's values in the log, even if those values are not null.

  • 1
    if you specify the related object in the debug log, it displays the info (ex. userRecord.Contact). It just won't display it if you only include the User object to debug (will only display user fields). May 21, 2020 at 14:56

You must log in to answer this question.

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