+ Start a Discussion
RevanthRevanth 

Soql joins to Get the ids of the parent objects

Please help me.I need to get the ids of the parend object with a condition in the grandchild object records.

 

note: We should use only one query(can have inner queries) and should not use maps and sets to first get the child records and then get the grandchild records.

 

Thank You 

SeAlVaSeAlVa

You can try to query the grandchild, using your conditions, and then taking out the grandparent id by navigating through the relationships with grandChildLookupField__r.childLookupField__c for example.

 

Or try to do a query in the middle (child table), with a sub-query you will access the grandchild records.

 

Regards.

RevanthRevanth

If i do as you say, the compailer is throwing  the error as you cannot use nest query for the semi-joint

SeAlVaSeAlVa

Then, query your grandchild, and take parent's parent id, something like 

[
   SELECT
      bRef__r.aRef__c
   FROM
      cObject__c
   WHERE
      conditions for grandchild object 
   GROUP BY 
      bRef__r.aRef__c
];

(cObject__c has a lookup or master-detail to bObject and is called bRef__c)
(bObject__c has a lookup or master-detail to aObject and is called aRef__c)

Raj.ax1558Raj.ax1558

Try this -

 

(SELECT id FROM Aobject WHERE ParentId =: (SELECT id FROM Bobject WHERE ParentId =: ( SELECT id FROM Cobject WHERE id =: key)));

 

Please Mark as solution for help other persons in a same query.

 

Thank You

Raj Jha

RevanthRevanth

Thank You sir,But here we are trying to get the grandchild records ids from grandchild-child-parent relationship.But i want to go from parent to child and then go to grandchild from child and give a condition for grandchild and with this condition i need to get the parents records ids.

 

correct me if i am wrong.