2

Below is the test class which creates Account and Contact,Community User as Sys Admin and test class runs as Community User.

And issue comes when am trying to access Account and Contact from Custom Controller as Account and Contact is set to private under Sharing Settings

  Account a = new Account(Name='Test Account Name');
  insert a;

  Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
  insert c;

  User user = new User();
  user.ProfileID = [Select Id From Profile Where Name='CustomerCommunity User Profile'].id;
  user.EmailEncodingKey = 'ISO-8859-1';
  user.LanguageLocaleKey = 'en_US';
  user.TimeZoneSidKey = 'America/New_York';
  user.LocaleSidKey = 'en_US';
  user.FirstName = 'first';
  user.LastName = 'last';
  user.Username = '[email protected]';   
  user.CommunityNickname = 'testUser123';
  user.Alias = 't1';
  user.Email = '[email protected]';
  user.IsActive = true;
  user.ContactId = c.Id;

  insert user;

  System.RunAs(user) {
    // do all of my tests as
  }

conList and accList are getting as null as Community user doesn't have access to account and contact.

    public void getContactDetails(){

      conList = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,Contact_Type__c,AccountId
                  FROM Contact where AccountID=:currentUser[0].AccountId];

   }


  public void getAccountDetails(){
    accList = [SELECT id, name, BillingStreet, BillingCity, BillingState, BillingPostalCode, 
   website,phone  FROM Account where id=: currentUser[0].AccountId]; 
    }

Updated: I tried to query Contact and Account and I can access it in my test method whereas In my Custom Controller am not able to access Contact or Account.

  @isTest
public class CommunityUser_Test
{
    /*
    * This method prepares the test data of this class.
    */
    @testSetup static void prepareSetupData() 
    {
          Account a = new Account(Name='Test Account Name');
          insert a;

          Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
          insert c;

          User user = new User();
          user.ProfileID = [Select Id From Profile Where Name='Customer Community User'].id;
          user.EmailEncodingKey = 'ISO-8859-1';
          user.LanguageLocaleKey = 'en_US';
          user.TimeZoneSidKey = 'America/New_York';
          user.LocaleSidKey = 'en_US';
          user.FirstName = 'first';
          user.LastName = 'last';
          user.Username = '[email protected]';   
          user.CommunityNickname = 'testUser123';
          user.Alias = 't1';
          user.Email = '[email protected]';
          user.IsActive = true;
          user.ContactId = c.Id;
          insert user;
    }


     static testMethod void testforActualScenarios()
    {
        User userObj = [SELECT Id,ContactId, AccountId FROM User WHERE Username='[email protected]'];
        System.RunAs(userObj) {
            //retrieve the contact.
            Contact contactObj = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,AccountId
                  FROM Contact where Id=: userObj.ContactId];

            System.assertEquals(contactObj.LastName,'Contact Last Name'); 


            List<Contact> conList = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,AccountId
                  FROM Contact where AccountID=:userObj.AccountId];

            System.assertEquals(conList[0].LastName,'Contact Last Name'); 

            //retrieve the account
             Account acct = [SELECT Id, Name FROM Account WHERE Id=:userObj.AccountId];
            System.assertEquals(acct.Name,'Test Account Name'); 

        }

    }
}
0

1 Answer 1

2

Customer Community User profile can only have READ and EDIT access to Account. So this user cannot create the account record.

According to sharing settings, at your org, OWD is Private. So the account record which SysAdmin has created will not be accessible to other users.

It will be good option to create test records in @testSetup and use those records in the test methods of that test class.

Secondly, in the User object AccountId is readonly, only you can assign ContactId. After creation of user, AccountId will be assigned to user based on the Contact Id.

See, my workable code.

@isTest
public class CommunityUser_Test
{
    /*
    * This method prepares the test data of this class.
    */
    @testSetup static void prepareSetupData() 
    {
          Account a = new Account(Name='Test Account Name');
          insert a;

          Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
          insert c;

          User user = new User();
          user.ProfileID = [Select Id From Profile Where Name='Customer Community User'].id;
          user.EmailEncodingKey = 'ISO-8859-1';
          user.LanguageLocaleKey = 'en_US';
          user.TimeZoneSidKey = 'America/New_York';
          user.LocaleSidKey = 'en_US';
          user.FirstName = 'first';
          user.LastName = 'last';
          user.Username = '[email protected]';   
          user.CommunityNickname = 'testUser123';
          user.Alias = 't1';
          user.Email = '[email protected]';
          user.IsActive = true;
          user.ContactId = c.Id;
          insert user;
    }


     static testMethod void testforActualScenarios()
    {
        User userObj = [SELECT Id,ContactId, AccountId FROM User WHERE Username='[email protected]'];
        System.RunAs(userObj) {
            //retrieve the contact.
            Contact contactObj = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,AccountId
                  FROM Contact where Id=: userObj.ContactId];

            System.assertEquals(contactObj.LastName,'Contact Last Name'); 


            List<Contact> conList = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,AccountId
                  FROM Contact where AccountID=:userObj.AccountId];

            System.assertEquals(conList[0].LastName,'Contact Last Name'); 

            //retrieve the account
             Account acct = [SELECT Id, Name FROM Account WHERE Id=:userObj.AccountId];
            System.assertEquals(acct.Name,'Test Account Name'); 

        }

    }
}

Update based on comments

Declare the controller as without sharing and run the test class. It will solve the issue.

0

You must log in to answer this question.

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