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
Chris Collier 6Chris Collier 6 

SOQL query on three objects (two standard and one custom)

I'm trying to query across three objects:
  • Account
  • Contact
  • Transaction (custom)
Some notes:
  • The relationship from Transaction to Account is AcountId__r
  • The relationship from Account to Contact is Contacts (correct me if I'm wrong)
  • Name is a field on Transaction
As a start, I'm trying to get:
  • Names of all Transactions
  • Names of the associated Accounts
  • Ids of the Contacts associated with those Accounts

Trying the following:
SELECT Name, AccountId__r.Name, AccountId__r.Contacts.Id FROM Transaction__c

I get:
ERROR at Row:1:Column:33 Didn't understand relationship 'Contacts' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Trying this:
SELECT Name, AccountId__r.Name, (Select AccountId__r.Contacts.Id FROM AccountId__r) FROM Transaction__c

I get:
ERROR at Row:3:Column:6
Didn't understand relationship 'AccountId__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

What is the correct way to write this query?
PriyaPriya (Salesforce Developers) 

Hi 

Try this 
 

SELECT Name, AccountId__r.Name, AccountId__r.ContactId FROM Transaction__c