2

I have two objects

Request__c and Matter__c

These two objects have a lookup relationship where Request__c is a lookup field on Matter__c. So a single Request record can have multiple Matter records.

I’m trying to write a SOQL query on the Request to pull all Matters associated with each Request. My query is as follows:

SELECT Id, Name, (SELECT Id, Status__c, Matter_Type__c FROM Matters__r) FROM Request__c

But whenever I try running this I get the following error:

Didn't understand relationship 'Matters__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.

I thought that if there was a lookup field on the Matter__c object, then a relationship between the 2 objects would exist and I could query to pull all the children (Matters) for a given Request.

I’ve checked the field in Setup -> Create -> Objects -> Matter__c and it provides the following information:

Related List Label: Matters

Child Relationship Name: Request

Can someone help me sort this out?

  • 1
    If Matters is a child of Request__c, the child relationship name Request is a poor choice and now would be the time to change it to Matters before you bake it into Apex code. – cropredy Jul 7 '16 at 4:54
  • oh I totally agree. I didn't set it up, but inherited it. But it definitely added a couple hours to me scratching my head. Thanks! – Clink Jul 8 '16 at 18:08
6

You can get the API Name of the child relationship as follows via Execute Anonymous:

for (ChildRelationship relationship : SObjectType.Request__c.getChildRelationships())
    if (relationship.getChildSObject() == Matter__c.sObjectType)
        system.debug(relationship.getRelationshipName());

Copy this value verbatim and replace Matters__r.

0

Try this:

SELECT Id, Name, (SELECT Id, Status__c, Matter_Type__c FROM Request__r) FROM Request__c

Inner query uses the relationship name which you can find on Matters object's lookup field.

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.