0

I want child record id with associated parent record id (e.g parent id=a8u540000004CWVAA3). I need all the associated child record ids given that parent Id

SELECT Name,id, (SELECT ID FROM CHILD OBJECT__R)  FROM parent object__c  where id IN ('a8u540000004CWVAA3','a8u540000004CWBBC4')
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
user33793
  • 21
  • 3

3 Answers3

1

Take the id's of parent object records in a list

List<parentobject__c> pic = new List<parentobject__c>();

pic.add(/*place your ID here*/);

Use the query Like below...

[SELECT Name,id, (SELECT ID FROM CHILDOBJECTS__R) FROM parentobject__c where id IN: pic]

Note: you have to place the plural of child object name appending with __r.

If your child object name is Student__c, then you have to place Students__r while using the subqueries.

Hope this might helps...

Subhash
  • 1,716
  • 1
  • 14
  • 26
  • Hi please see below ,gives me an error plz assist list<Quote_Project__c>pic=new list<Quote_Project__c>(); pic.add('a8uE0000000gItP');

    [SELECT Name,id, (SELECT ID FROM cell_Block__R) FROM Quote_Project__c where id IN: pic]

    – user33793 Jun 26 '16 at 22:37
  • You have to give cell_blocks__r as I mention in my answer NOTE. Plural must be given for custom object subquery – Subhash Jun 27 '16 at 01:18
0

Your query seems to be correct.

If you want to list down only the Id fields from child then use this as below:

SELECT ID FROM CHILD_OBJECT_c WHERE parentId in ('a8u540000004CWVAA3','a8u540000004CWBBC4')

You can refer this link relationship queries

Santanu Boral
  • 36,003
  • 8
  • 41
  • 71
0

i tried all the above queries doesn't work please assist

listpic=new list(); pic.add('a8uE0000000gItP'); [SELECT Name,id, (SELECT ID FROM cell_Block__R) FROM Quote_Project__c where id IN: pic]

Error=Unknown error parsing query

user33793
  • 21
  • 3
  • ahh!! Are you trying this in Query Editor in developer console? Query editor only takes in direct SELECT statements only. Where are your trying the code? – sfdcFanBoy Jun 27 '16 at 01:59