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
jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12 

How to query detail records from SOQL

Hi.

I have a custom object (TargetX_SRMb__Application__c) It has a relationship to Contact defined in WSDL by:

  <complexType name="TargetX_SRMb__Application__c">
                <complexContent>
                    <extension base="ens:sObject">

...
                       <element name="TargetX_SRMb__Contact__c" nillable="true" minOccurs="0" type="tns:ID"/>
                        <element name="TargetX_SRMb__Contact__r" nillable="true" minOccurs="0" type="ens:Contact"/>

However, when I run SOQL test as:

 Contact contact = [SELECT  c.Id, c.lastName, (Select CreatedDate  FROM TargetX_SRMb__Contact__r) FROM Contact c]

I get error:

Error: Compile Error: Didn't understand relationship 'TargetX_SRMb__Contact__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

I also tries adding a "s" as follows: Same error.

 Contact contact = [SELECT  c.Id, c.lastName, (Select CreatedDate  FROM TargetX_SRMb__Contacts__r) FROM Contact c]

 Is this not the correct syntax to query child records from a custom object?

Thanks in advance,

Jon
Kiran  KurellaKiran Kurella
The relationship must be a Master-Detail relationship and make sure you use the correct relationship name.
asish1989asish1989
Check relationship name first. 

1.Go to this object TargetX_SRMb__Contact
2.click on look up /master detail field to Contract 
3.Check relationshipname 
4.Then append __r to relationship name

jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12

Thanks asish1989. The relationship name is Application so if I append __r to this I still get same error.

@isTest
private class TestClass {
       static testMethod void validate() {
      Contact c = [SELECT  Id, lastName, (Select Id FROM Application__r) FROM Contact ];

    }
}

 

Error Error: Compile Error: Didn't understand relationship 'Application__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. at line 5 column 19
Error  



 

Master-Detail Options


jonpilarski1.3914448382645588E12jonpilarski1.3914448382645588E12
One other thing. I believe this has something to do with the custom object being "managed" as I cannot change the relationship name. It is not enabled. If I use the same syntax on a custom object that is not managed it compiles fine. USD_Eform is custom object not managed.

   Contact c = [SELECT  Id, lastName, (Select Id , Eform_Type__c FROM USD_Eform__r) FROM Contact ];
asish1989asish1989
Hi 

Contact c = [SELECT  Id, lastName, (Select Id FROM Application__r) FROM Contact ];

if Application__c is a object from manged package then append package prefix name to that relationship name.

for example write package_prefix_Application__r in stead of Application__r.
Vee_RVee_R
If TargetX_SRMb__Application__c is your custom object and child to Contact, use the below inner query. We should make use of plural of custom object name in the query

SELECT  c.Id, c.lastName, (Select CreatedDate  FROM TargetX_SRMb__Applications__r) FROM Contact c
Prady01Prady01
Hi, why dont you get the query from eclipse, This way you dont need to write it, Eclipse will make the query for you, Make a Force.com project in eclispe and then you see a file called salesforce schema, Click on in and start selecting the objects and childs (a joke) fathers :D

Attached eclispe screen shot

User-added image