I'm trying to run an Apex test case as a Community User. I've populated my data inside a @TestSetup method and when I run the test case User Record has a ContactId but there is no Contact Record associated to the User's ContactId. Here is my code block.

    static void setupData() {

        Id profileId = [select id from profile where name=: 'Partner Community User'].id;
        Account ac = new Account(name = 'ACME Co') ;
        insert ac;

        Contact con = new Contact(LastName = 'Test_Contact'
        ,AccountId = ac.Id
        ,ANEM_BState__c = 'Paris'
        ,ANEM_BCountry__c = 'France'
        ,Email = '[email protected]'
        ,Marketing_Status__c = 'Active'
        ,Should_receive_pricing_communications__c = 'No'
        ,Account_type__c = 'Broker'
        ,Title = 'Journalist');
        insert con;


        User communityUser = new User(alias = 'comUser'
        ,email = '[email protected]'
        ,lastname = 'Batman'
        ,profileid = profileId
        ,country = 'France'
        ,IsActive = true
        ,ContactId = con.Id
        ,username = '[email protected]');
        insert communityUser;

     static void testMyProfileLogicForFetchAccounts() {
     List<User> lstUsers = [SELECT Id,ContactId,ANEM_BCountry__c FROM 
     User WHERE username =: '[email protected]' LIMIT 1];
     //These debug logs have values
     system.debug('@mal-user'+lstUsers[0].Contact.ANEM_BCountry__c); //has a value
     System.debug('@mal-contact-Id:'+lstUsers[0].ContactId);//has a value
     //Logic to check

    ---------------------------Actual Logic-----------------------------------
    //Contact Query is giving me null values to User's related contact
    public class WhoIsWhoController{

        public static List<ContactWrapper> getwhoIsWhoContacts() {

    System.debug('@mal-User-NAME'+UserInfo.getName()); // Giving me correct community user's name
            User communityUser   = [SELECT Id,Name,ContactId, Contact.ANEM_BCountry__c, Contact.ANEM_BState__c, Email FROM User WHERE Id = :UserInfo.getUserId() LIMIT 1];

            //for debug
            System.debug('@mal-CONT_ID:'+communityUser.ContactId); // has a Id value
            System.debug('@mal-Contact.Name:'+communityUser.Contact.Name); //Giving me null
            System.debug('@mal-Contact.ANEM_BState__c:'+communityUser.Contact.ANEM_BState__c);//Giving me null



I've checked the profile permissions also. All are full accessible. Any Idea guys, why I'm getting null values for User's Contact Fields ?

  • communityUser.Contact.Name
  • communityUser.Contact.ANEM_BState__c

Appreciate your help, Thanks.

1 Answer 1


It's likely this is a sharing issue rather than a profile (crud) issue. To verify, append the without sharing keyword to your WhoIsWhoController as so:

public without sharing class WhoIsWhoController {
// ... logic here

Run your test again and, if the Contact record becomes visible to the community user, it is indeed a sharing issue. If that's the case, you can leave your class as-is (without sharing), or adjust your sharing schema as necessary.


You must log in to answer this question.

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