I have a Custom Object Rule__c, And I have added another Custom object Rule_Set__c as Related list in page layout for Rule__c and when I try to query like below:

    public static List<Rule__c> getRules(String objId) {
        String query;
        query = 'SELECT Id, Name, Formula__c';
        query += ' ,(SELECT Name' + ' FROM Rule_Sets__r)';
        query += ' From Rule__c WHERE Active__c = true AND Parent_Obj__c = :objId';
        return Database.query(String.escapeSingleQuotes(query));

I am getting Exception like:

Error: Didn't understand relationship 'Rule_Sets __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.

Note: When I query for fields for another Custom object which is related to the Rule__c Object the above query is returning results properly, I am getting the Query Exception only when I query for fields in object Rule_Set__c

  • 3
    Check what you set the relationship name to through the Object Manager in the setup UI.
    – Phil W
    Aug 24, 2020 at 19:37

1 Answer 1


The Child Relationship Name is the API name of a list of children records under the parent of the lookup.

Your query for the parent can include a subquery for these child records, using this child relationship name you can even iterate these records in a loop as it is basically a list of child records mapped to each parent record.

Go to Setup --> object manager --> select the child object --> search and select the lookup/master-detail relationship field to Rule(parent object) in fields and relationships tab --> check the child relationship name.

You can use this child relationship name in query by appending suffix "__r" to it. If your relationship name is tests then you can use tests__r in query or while looping though these records.

**Note: we need to append "__r" only in case of custom relationship. If we are using standard relationship then no need to append it like in case of account and contacts where contacts is the child relationship name of contact for account.

  • Arpit -- forum etiquette is to NOT solicit for answer selection.
    – cropredy
    Aug 24, 2020 at 21:25
  • @cropredy The have removed my comments.
    – Arpit
    Aug 25, 2020 at 4:51

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