+ Start a Discussion

Query from child records

here i have 3 objects. A, B and C , here A is the parent to B and B is the parent to C.
now my requirement is to write query on A and then i want to get childs  B  and C records.
is it possible to query on a single query.

suggest me the solution.
thanks in advance,
Shingo YamazakiShingo Yamazaki
As long as I know, subquery can go only one level deep.
So if you query from top level object A like this,
List<A__c> records = [
  SELECT Name,
    (SELECT Name,
      (SELECT Name FROM C__r)
    FROM B__r)
  FROM A__c];

It would raise following error.

> SOQL statements cannot query aggregate relationships more than 1 level away from the root entity object.

One possible solution is, querying from middle level object B like this,
List<B__c> records = [
  SELECT A__r.Name,
    (SELECT Name FROM C__r)
  FROM B__c];

I'm not sure if this can be used in your situation, but just for reference.