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')
  • 2
    select name, id from contact where accountid in ('a8u540000004CWVAA3','a8u540000004CWBBC4') select name, id from contact where accountid in (select id from account where name like 'Alice%') – Ilya Lepesh Jun 26 '16 at 2:25
  • 4
    Even though the objects are different, this question is almost identical to How to Query all opportunities ids from account – Adrian Larson Jun 26 '16 at 9:27
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...

  • 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 1: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

  • I tried above query doesn't work – user33793 Jun 26 '16 at 23:02
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

  • 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 1:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.