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
CamzillaCamzilla 

Display Relationship Query Result

Im not using a sandbox but testing towards the actual database. This is the query I'm trying:

 

Select (Select Id, CurrencyIsoCode, Postcode_lookup__c From Post_code_company__r) From Postal_code_lookup__c 

 

The result Im looking for is not objects itselfs, but I want a list of the child records. Im not sure how to acomplish this since there seem to be no __r object to getch data from? How can you do this query?

Best Answer chosen by Admin (Salesforce Developers) 
CamzillaCamzilla

I figured it out myself, to access the children you can search through a relation, but since the realtion compares with a whole object you have to specify a field. Like so;

 

[Select  Id, CurrencyIsoCode, name from PC_Company__c where Postcode_lookup__r.Name = :searchString]

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Camzilla,

 

You can query the child object's records as follows,

 

Parent__c p = [Select id, name from Parent__c where <Condition> limit 1];  //Query the parent record id first
List<Child__c> childList = [Select id,name,ParentID__c from Child__c where ParentID__c =: p.id); //List of all childs__c of the parent__c (p.id)

 

So, now childList will have all the records that matches the parentID__c as p.id.

 

Hope this will help you...!

 

Please don't forget to give kudos and mark this as a solution, if this works out.

 

 

 

 

jungleeejungleee

It is the child relationship name that should be mentioned in the inner query and not he name of the child object itself. 

 

If you open to the lookup field in the child object, you will get the child relationship name which you should use in the inner query.

 

 

select name (select name from child_object_relationShip_name__r) from parant_object__c

 

 

CamzillaCamzilla

Hey KamatchiDeviR,

I have more than one child, so your query doesnt really work for me.

CamzillaCamzilla

Yo jungleee,

I know this already, and thats what i tried. You'll get a result list of an object with its children..

CamzillaCamzilla

I figured it out myself, to access the children you can search through a relation, but since the realtion compares with a whole object you have to specify a field. Like so;

 

[Select  Id, CurrencyIsoCode, name from PC_Company__c where Postcode_lookup__r.Name = :searchString]

This was selected as the best answer
jungleeejungleee

I was not sure if you were aware of that. Anyways here's what you wanted;

 

 

for(parent_object__c po: [select name (select name from child_object_relationShip_name__r) from parant_object__c]){
   list<child_object__c> childObjectList = po.child_object_relationShip_name__c;
system.debug('the child object is '+ childObjectList);
}