0

I'm trying to construct a query that takes a specific Contract with a provided Id, and I want to fetch from this record, the related Order's, and their OrderItem's,

So If I understand correctly I would need to do as a first instance, where sid is the provided ID.

SELECT Id,
       Name,
       Account.Name, 
       (SELECT Id, Name FROM Order)
FROM Contract
WHERE ID = :sid

And the Error I get, is the following, but so far, I don't understand If I'm constructing the query in the wrong way, or what am I doing wrong in general with this, I want to avoid multiple SOQL, because of a POST-Query Manipulation I want to save EXEC-time, with doing only one query.

       (SELECT Id, Name FROM Order)
                             ^
ERROR at Row:4:Column:30
Didn't understand relationship 'Order' 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.

Any Idea, how can I do this better?

2

This is the right query

SELECT Id,
        Name,
        Account.Name,
(SELECT Id, Name FROM Orders) 
FROM Contract

Note the plural "s" after the word Order

Since you're querying for children, you have to pick the relationship name, not the object. For standard relationships, this is usually a plural of the object name (this will be likely different for custom relationships)

  • 1
    This is so weird, so, for the standar objects when ever I do children query like this I need to use the Plural for Standar Objects and the __r for custom? – ekiim Dec 4 '18 at 0:00
  • Standard objects are a little different, since their names are hardcoded in the app. Custom relationhips afford you some control. You normally use __r when you go from child-to-parent. And use the plural when you go from parent-to-child (via subquery). – Sebastian Kessel Dec 4 '18 at 0:05

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.