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
aiyaz_SFDCaiyaz_SFDC 

SOQL Query question (Parent to Child)

Hi,

 

 

I have a custom object (Bonds) that has a lookup field that displays Accounts which creates the Child-to-Parent relationship.

 

Currently i'm working with Visualforce to display Accounts detail along with just the current month's Bonds.  When I view the WSDL I see that Bonds is set as a related object (Bonds__r).  When I set up a SOQL query i'm unsure whether I am on the right track although I believe I should be using the "__r" notation when referencing the relationship.   Below is the query: 

 

 

 

Account[] ab =
[SELECT Name, (SELECT BondVol__c FROM Bonds__r) FROM Account]

 

 

mtbclimbermtbclimber

In order to distinguish between a custom field name and a custom relationship name we have use a different suffix. Fields and objects get "__c" and relationships get "__r". Again, this applies to custom relationships (and fields/objects) only.

 

The enterprise WSDL and API Describe calls describe all the names as you should use them in Visualforce and Apex.

aiyaz_SFDCaiyaz_SFDC

Thanks for the tip. 

 

With that in mind, i'm assuming it makes sense on how i queried the BondVol__c field from the Bonds__r custom relationship (related to Account) ?

 

[SELECT Name, (SELECT BondVol__c FROM Bonds__r) FROM Account]

 

mtbclimbermtbclimber
The syntax looks good. Without knowing your schema it's hard to say whether it's actually valid though.  You can test your queries with the force.com IDE and, if you use mac there's a lighter tool called soqlExplorer which I use.