I quered user in test class using user id= UserInfo.getUserId() but i need contact id this is possible or not

User us = [
    select Id, Contact.accountId, ContactId
    from User
    where Id =: UserInfo.getUserId()
UserContact = [
    select Id, AccountID
    from Contact
    where Id=:us.ContactId

UserInfo.getUserId()this id admin userid palese anybody help me.

  • 1
    If you want to have a user with a contact Id you would have to create a community user and use the runAs function rather than just query from an admin user account. – Dave Humm Jan 29 '18 at 10:22

The ContactID on the User table is only valid for Partner or Customer Community Users. For internal Salesforce Users, there is no Contact associated with a User record.


Insert Account, Contact and User then you will get Contact Id on User for test Class

  • 1
    i think UserInfo.getUserId() getting the admin only – Ramesh M Jan 29 '18 at 9:37
  • 1
    admin id don't have contact id – Ramesh M Jan 29 '18 at 9:38

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.