2

I have a relationship between objects as below:

User->Contacts->Account->Parent

I am trying to get the parent id using the following SOQL query:

[Select user.contacts.account.parentid from user where Id="15 digit id"];

But I get an output as query,query. I tried going one level up by using this SOQL:

[select contact.account.parentid from contact]; 

and I get the required parent id. I am not sure why it does not work in the first query when I go one level deeper in the object relationship.

7
  • 1
    User has many contacts ? Commented Feb 23, 2016 at 19:40
  • 1
    this is because the tool you are using to display the results has to traverse a 1:n relationship and doesn't have an easy way to show multiple rows of results; the query from a single Contact only has one result - hence displayable
    – cropredy
    Commented Feb 23, 2016 at 19:40
  • User has only one contact ( does it matter if user has different contacts?)
    – SfdcBat
    Commented Feb 23, 2016 at 19:41
  • Then why are you using user.contacts instead of user.contact ? Commented Feb 23, 2016 at 19:42
  • @crop1645 Is there any alternative query which I can use in my Developer console to view the parent id?
    – SfdcBat
    Commented Feb 23, 2016 at 19:42

1 Answer 1

1

If Contact is a PARENT of user, then User.Contact (instead of Contacts) should do the trick.

If it is a CHILD, then you can't really query like that, because you may have many contacts for a given user. In that case, the ideal query would be this:

[select User.Id,
        (select contact.account.parentid from contacts)
   from User
   where Id = 'XXX'];

That will give you ALL the parentIds for ALL the contacts.

7
  • 1
    FYI you can get the relationship name programmatically: system.debug(User.ContactId.getDescribe().getRelationshipName());
    – Adrian Larson
    Commented Feb 23, 2016 at 19:52
  • contact. is superfluous
    – cropredy
    Commented Feb 23, 2016 at 20:11
  • @sebastian - I guess there is no master detail or lookup relationship involved here , it just user are related to contact and contact are related to account. User has only one contact.
    – SfdcBat
    Commented Feb 23, 2016 at 21:23
  • Well, that's impossible (the lack of relationships). It simply must be implied. Use User.Contact (not User.Contacts) or you can get the relationship name from the sample on @AdrianLarson 's post Commented Feb 23, 2016 at 21:24
  • Its actually one to one relationship between user<->contact<->account<->parent.
    – SfdcBat
    Commented Feb 23, 2016 at 21:28

You must log in to answer this question.

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