-1

Hi This query doesnt work for me, I dont know what I am missing that I cant query the recent Contact's entry by user that just logged in to salesforce account.

Id accountid = userinfo.getProfileId();
     myConn = [SELECT Id, Name, Account.Name , Accountid 
                         from Contact 
                         WHERE Id = :accountid ORDER BY  Name ASC Limit 10];
5
  • Do you think that maybe a "ProfileID" would not match ANY Contacts since they are not the same thing??? I think what you want to do is get the user ID, query for the user and get their contactID (Assuming it is a portal/community user) then you can use that ID to query the contact. But then you would only return one contact so not sure what you are even trying to do with the above code
    – Eric
    Commented Sep 23, 2015 at 16:48
  • @Eric ye this is for the Community User. so imagine a user that has a salesforce account has created few new contact,. I even UserInfo.getUserId(); did this insted of GetProfileID and didnt work , no results was returned
    – Metawaa
    Commented Sep 23, 2015 at 17:17
  • 1
    @bez You are getting yourself confused. getProfileId returns an Id for a Profile record, which can never be a Contact Id. getUserId returns the Id of a User record, which can also never be a Contact Id.
    – Adrian Larson
    Commented Sep 23, 2015 at 17:22
  • You query for the user record by using UserInfo.getuserId (This will give you the id the current logged in User). That user would have a contactid associated( i dont remember the exact api name). Use that id to query the contact record. In your case instead of using accountId, use the contactId of the associated user record
    – Prady
    Commented Sep 23, 2015 at 17:22
  • Sorry, i misunderstood the question. I thought you wanted to get the contact record associated with the user. @smckitrick answer should work for you
    – Prady
    Commented Sep 23, 2015 at 17:33

1 Answer 1

1

Your existing query is trying to match the Contact ID (starts with 003) with your User ID (starts with 005). Your list will never show a matching record.

You need to change your query to link the Owner ID to the current user. That will give you a matching value, if there are any contacts that are linked to that owner.

myConn = [
    SELECT Id, Name, Account.Name , Accountid 
    FROM Contact 
    WHERE OwnerId = :userinfo.getUserId()
    ORDER BY  Name
    ASC LIMIT 10
];
1
  • 1
    Thank you Adrian. This is definitely cleaner. My original solution was more to show him where his error was at.
    – smckitrick
    Commented Sep 23, 2015 at 17:53

You must log in to answer this question.

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