0

I have 3 objects, Object A, Object B and Object C. Object B is a child of Object A, while Object C is a child of object B. I am trying to access the all the single earliest records of all records in Object C that are related to object A through object B through SOQL code. Any clues? Here are some of the SOQL queries I've come up with but they only get the earliest single record from Object C related to Object B

Object A -> Object B -> Object C

 EarliestRecordsObjectCOnly = [SELECT Name, ObjectB__c, Meeting_Date__c FROM ObjectC__c WHERE ObjectB__c =:id ORDER BY Meeting_Date__c ASC LIMIT 1];
//these SOQL queries get the earliest single record from Object C related to Object B

Tried the SOQL code below and I get the error that Didn't understand relationship 'ObjectC__c' in field path

EarliestRecordsObjectCOnly  = [SELECT Id, (SELECT ObjectB__c,  Meeting_Date__c FROM ObjectC__c WHERE ObjectB__c.ObjectA__c =:id ORDER BY Meeting_Date__c ASC LIMIT 1) FROM ObjectA__c];

Any ideas? Let me know if there I need to be clearer. Thanks in advance

1 Answer 1

0

I don't have the right data structure setup anywhere to test this, but this should work:

 ObjectB__c[] list = [SELECT Name,
                         (SELECT Name, Meeting_Date__c 
                          FROM ObjectC__r 
                          ORDER BY Meeting_Date__c ASC LIMIT 1 )
                     FROM ObjectB__c 
                     WHERE ObjectB__c =:objectAId 
                     ORDER BY ObjectBDateField__c ASC LIMIT 1];

Note I have a list as the top level item - it's much safer to query with a list, even if you have a LIMIT 1 clause. From here, you should be able to access your items like so:

ObjectC__c = list[0].ObjectC__r

You may want to remove the LIMIT 1 from the subquery also and just get a list, where you can extract the 0th indexed property.

Hope this helps!

Oh, it's possible there's a better way to do this - but I do know this should work.

Note Parent-Child relationships use the PLURAL form of the object name, ie in the below example, Daughter__c becomes Daughters__r

//Child to parent
SELECT Id, FirstName__c, Mother_of_Child__r.FirstName__c
FROM Daughter__c
WHERE Mother_of_Child__r.LastName__c LIKE 'C%'

//Parent to child
SELECT LastName__c,
  (
   SELECT LastName__c
   FROM Daughters__r
  )
FROM Mother__c
4
  • Hi CaspNZ , I get the following error when I try that 'ObjectC__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 13 column 21
    – Rufus K.
    Apr 13, 2015 at 9:24
  • If you are going parent->child using standard objects, the relationship is the plural of the object name, eg SELECT Name, (SELECT LastName FROM Contacts) FROM Account (in this case Contacts). If parent-> child using custom objects, you use __r rather than __c that you'd normally use for the object name. Refer here for more info: salesforce.com/us/developer/docs/officetoolkit/Content/… Apr 13, 2015 at 9:57
  • Hey CaspNZ, I'm trying to access the grandchild of the parent object not the child object. when I try use ObjectC__r or ObjectC__c, it generates the same error
    – Rufus K.
    Apr 13, 2015 at 12:49
  • I realize this. The child is selected for in the outer WHERE clause and the grandchild is found in the inner clause. The issue might be how you name your relationship. You need to use the plural version of your Object name, ie for an object called 'Document' and plural 'Documents', the relationship name might be 'Documents__r' - note the extra 's' Apr 13, 2015 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .