0

I have a query I've written for the Organization/Account object

SELECT ID, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, County__c, Phone, ParentId FROM Account

This works fine and returns what I need, however ParentId is a hierarchy column that references the same object - Account

I can't seem to figure out a way to write a query that returns the data about the record that ParentId refers to. All I really want is the "Name" associated to that ParentId.

Currently I do a second query and then match the results to each other, but this seems inefficient. Can I do it in pure SOQL?

1 Answer 1

1

Where with custom relationship fields you turn My_Custom_Lookup__c into My_Custom_Lookup__r.Some_Field__c for cross-object SOQL, with standard fields you typically just chop off the Id on the end of the field. So here,

SELECT Parent.Name FROM Account

Or likewise,

SELECT Account.Name FROM Case
SELECT Opportunity.Amount FROM OpportunityContactRole

where the lookup fields are AccountId and OpportunityId respectively.

You must log in to answer this question.

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