I have a related list on my Contracts for Orders. I am querying for certain contracts, and I want to Select ID, startdate, and some other fields, along with its corresponding related list of orders. I've seen some code for grabbing related lists, but I can't seem to find what the list API name is, or correct syntax from other forums for something to work. Can somebody step me through this, starting from where I find the related list API name? I think I recall going to 'fields' one time in the past and seeing other relationships there, but not in this case now.


enter image description here


3 Answers 3


You can always find the child relationship name by going through the DescribeSObjectResult on your parent object.

DescribeSObjectResult describe = SObjectType.Contract;
for (ChildRelationship relation : describe.getChildRelationships())

You can just filter this debug log for Order (case-sensitive) and you will find the relationship you are looking for. You want the value in getRelationshipName.

If you wanted to use the Contract and Order standard objects, you would do:

for (ChildRelationship relation : SObjectType.Contract.getRelationships())
    if (relation.childSObject() == Order.sObjectType)

If that script outputs Orders, then you would do:

List<Contract> contractsWithOrders = [
    SELECT StartDate, (SELECT Id FROM Orders)
    FROM Contract WHERE ...
  • Thanks! I found the relationship name. It is "Orders", which is what I used in my query before (Orders__r) from another source, and it didn't seem syntactically correct. Now that I have this part verified, how do I go about querying for it when querying for contracts? Commented Feb 7, 2017 at 17:31
  • What do contracts have to do with orders?
    – Adrian Larson
    Commented Feb 7, 2017 at 17:32
  • See question: I have a related list on my Contracts for Orders I am trying to pull this related list along with other fields on the Contracts I'm querying Commented Feb 7, 2017 at 17:33
  • @NatalieSpatharakis - No __r, I was incorrect in my previous edit. Simply use From Orders in the subquery. To tell you exactly, we will need to see some code or better understand what you are trying to query
    – Eric
    Commented Feb 7, 2017 at 17:33
  • That's what I'm trying to find out, how to do a "subquery". I do not understand how to pull a related list from an object in a query Commented Feb 7, 2017 at 17:35

Declarative (Standard Order Object)

  1. Setup
  2. Customize
  3. Orders
  4. Click on the field that is the lookup to the Contract
  5. Look at the Child Relationship name

For code see Adrian's answer

An example query

 Select Id, ....., 
    (Select Id, ...... From Orders) 
 From Contract
  • Caveat that this approach doesn't provide the namespace.
    – Adrian Larson
    Commented Feb 7, 2017 at 17:17
  • There is no "Orders" under customize -- see edited question for screenshot. Thoughts? Commented Feb 7, 2017 at 17:28
  • @NatalieSpatharakis Is orders a custom object Orders__c If so goto Create -> Objects. Also, the list under customize is not in alphabetical order. you can type Order in the search box to find it if not custom
    – Eric
    Commented Feb 7, 2017 at 17:31
  • Orders are standard objects Commented Feb 7, 2017 at 17:33
  • Then it will be under the customize list. In my org is is right under Contracts
    – Eric
    Commented Feb 7, 2017 at 17:34

You can use Schema describe to get all of the child relationship API names:

String[] sObjTypes = new String[]{'Contract'};
Schema.DescribeSObjectResult[] describeSObjResults = Schema.describeSObjects(sObjTypes);
for(Schema.DescribeSObjectResult dsr : describeSObjResults) {
    for(Schema.ChildRelationship cr : dsr.getChildRelationships()) {
        system.debug( 'sObject: ' + dsr.getName() + ' - Child Relationship API Name: ' + cr.getRelationshipName());

Then to for the subquery to grab all related Orders to contracts:

[SELECT Id, (SELECT Id FROM Orders) FROM Contract]
  • 1
    Appreciate the effort but the answer does not provide anything additional to what Adrian posted. It actually adds unnecessary code. If you feel it adds to the previous answer can you please clarify the benefits of the additional code
    – Eric
    Commented Feb 7, 2017 at 17:36
  • I felt it would be useful to know you could get the describe information for multiple sObjects at the same time. Also, it doesn't sound like the OP is planning on actually using this in code, rather just to retrieve the API names as needed. I didn't see the latest edits from Adrian's post, I can remove my post if its not useful.
    – Bob Lopez
    Commented Feb 7, 2017 at 17:42

You must log in to answer this question.

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