2

I want to write this simple soql query where in Account there is a ParentId and with it would want to bring the name of this parent (Hierarchy field) Something like:

select Id, Name, (select Name from Account__r where Id = Account.ParentId)
From Account where id = [xxxxxxx]

I know my Where statement is wrong, just wanted to clarify what it is I want I have seen many examples of join but never where it is the same table.

  • I think you are looking for self join, is that right? – javanoob Dec 19 '18 at 20:13
2

You do not need any additional filters to make sure the children are related to the parent. If you write the below query, each row will have an attribute named ChildAccounts which contains each Account record whose ParentId matches the row's own Id value.

SELECT Name, (SELECT Name FROM ChildAccounts) FROM Account

If you want to select the parent record instead of children, you would instead do:

SELECT Parent.Name FROM Account
  • I think the result might be inverted from what I want. If I were to write it in standard SQL it would be: SELECT a.Id, a.Name, b.Name FROM Account a left Join Account b on a.ParentId = b.Id WHERE a.id = xxxxxx – Christian Paquin Dec 19 '18 at 19:41

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.