function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MandyKoolMandyKool 

Getting ContactId from User

Hi,

 

I need to get the ContactId from the User object. 

 

I have written the following code for it.

 

 

List<User> lstUser = [Select u.Id,u.ContactId from User u where u.Id = :idUser];
            if( lstUser != null && lstUser.size() > 0 )
            {
                System.debug('lstUser[0].ContactId::' + lstUser[0].ContactId);
                return lstUser[0];
            }

 

List<User> lstUser = [Select u.Id,u.ContactId from User u where u.Id = :idUser];            

if( lstUser != null && lstUser.size() > 0 )            

{

      System.debug('lstUser[0].ContactId::' + lstUser[0].ContactId);

      return lstUser[0];

}

 

but the System.debug statement prints as null.

 

If i try to execute the same query in the Force.com IDE, i got the correct result.

 

Also my class is running in System mode. 

 

Can anybody please tell me what i am doing wrong here.?

 

Best Answer chosen by Admin (Salesforce Developers) 
kanchan.123kanchan.123

Hi Mandar,

 

The reason that you are not able to fetch the contact Id with the above code is: This is fetching the contactId based on the User who is currently logged in. Only Users who are enabled for Customer Portal will have a contact lookup on the user detail page. Right now, when you are trying to fetch the contactId, you are the current logged in user but as you are not a customer portal user, you will not have contact lookup on your account.

 

Try logging in as a customer portal user, you will be able to fetch the contactId for the same using above code. Hope this Helps :)

 

 

- Kanchan

All Answers

Pradeep_NavatarPradeep_Navatar

Have you checked the userid using lstUser[0].id? Check whether the user exists with that contactid in org or not.

 

Elaborate which id(idUser) you have used in the where clause.

_Prasu__Prasu_

I too tried out Mandar's sample code. But ContactId is not accessible in Apex I guess. But when you use SforceExplorer or IDE schema browser ContactId comes in result.

 

Is it bug or security issue or Salesforce works in that manner?

kanchan.123kanchan.123

Hi Mandar,

 

The reason that you are not able to fetch the contact Id with the above code is: This is fetching the contactId based on the User who is currently logged in. Only Users who are enabled for Customer Portal will have a contact lookup on the user detail page. Right now, when you are trying to fetch the contactId, you are the current logged in user but as you are not a customer portal user, you will not have contact lookup on your account.

 

Try logging in as a customer portal user, you will be able to fetch the contactId for the same using above code. Hope this Helps :)

 

 

- Kanchan

This was selected as the best answer
MandyKoolMandyKool

Hi Kanchan,

 

Thanks for your help.You are correct, it was working fine when i logged in using the Customer Portal users credentials.

 

The solution worked like a charm.

 

Hey..looks like you sit in my ODC and you are working with me for last one month..

I should have come to your desk to ask you the same..

 

Anyway thanks for your help.

I will need your help in future, so plz keep checking if i post something.

 

Thanks,

Mandar.