public without sharing class UserDetails {
    public static Account Details()
     String UserId=UserInfo.getUserId();
     String contactId=utility.getUserContactId(UserId);  
     Account acc = [SELECT Id FROM Account WHERE PersonContactId =: contactId];
     return acc;
  • 2
    Hi, and welcome to SFSE! Please take a moment to read about How to Ask, and take the tour. Please don't state that your question is "urgent", as that is unhelpful. Also, what resources have you looked at so far? Where are you stuck? How can we help you? – sfdcfox Oct 9 '20 at 13:14
  • Hello, @sfdcfox i am a newbie, i am running this test method(getLoggedInUserId) with test data factory method getAccount but on executing the test method, test fails as well as i get a error i.e ''System.QueryException: List has no rows for assignment to SObject" – Anna Oct 9 '20 at 13:27

Based on the code that's provided and without a specific line number or method call that the error is coming from, I don't think that an Account or Contact is ever inserted within the unit test. Therefore there may not be a Contact to return from utility.getUserContactId(UserId). There may also not be any Accounts to return from either Community_TestDataFactory.getAccount nor Community_UserDetails.membershipDetails.

Insert a Contact and Account within your unit test block so that there is data to work with within the unit test.

I would also ensure your query cannot return more than one Account record based on the PersonContactId field (I am not sure if that's a unique field or not).

  • 1
    Put another way... Unit tests don't have access to most data in your org. In a unit test, you are responsible for creating the data required to run your test (which in this case would be an Account and a Contact at the very least). – Derek F Oct 9 '20 at 13:40

Just a bit of a hint. Whenever you try to write test methods to test your code, always follow the rule of AAA. i.e.

Arrange, Act, Assert

Arrange => create, insert and update your records upto the state in which your code will run.

Act => update your record in a way expecting the test will cover your code which you have implemented.

Assert => check for error messages that will fail the test if your code is not working properly.


My issue is resolved now by the code below.This code is very helpful when person account is enable in your org.

static void testM()
Account a = new Account();
       a.firstName = 'Nike';
       a.LastName = 'James';
       a.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Your recod type name').getRecordTypeId();
       insert a;
        Account acc= [SELECT id,Name From Account Where id =: a.Id ];
        Profile profileID = [SELECT Id, Name FROM Profile WHERE Name= 'your profile name'];
        List<Contact> lstContact = [SELECT Id FROM contact WHERE  accountId   =:  acc.Id];
        Contact con = lstContact[0];
        User u = new User();
        u.LastName = 'James';
        u.FirstName = 'Nike';
        u.Email = 'nike@g.com';
        u.Alias = 'James'+'00';
        u.Username = 'nike@g.com';
        u.CommunityNickname ='N' + ' '+ 'James';
        u.ContactId = con.Id;
        u.LocaleSidKey = 'en_US';
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.IsActive = true;
        u.ProfileID = profileID.id;
        u.LanguageLocaleKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';    
        insert u;
        User userForTesting = [SELECT Id FROM User WHERE Id =: u.Id LIMIT 1];

             String contactId=utility.getUserContactId(userForTesting.Id);  
             Account acnt = [SELECT Id FROM Account WHERE PersonContactId =: contactId];

  • If one of the answers posted gave you a solution, please consider accepting the answer by clicking the checkmark. This gives you and the answerer both a bit of rep. You do not need to post revised versions of your code. – David Reed Oct 9 '20 at 15:25

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.