function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Colin LoretzColin Loretz 

Parent-to-child relationship query

My objects are:

Object: Partner_Sent__c (object used for Many-to-Many)
Fields: CVB_Partner__c (Lookup to CVB_Partner__c object), Opportunity__c (Lookup to Opportunity object)

Object: Opportunity
Fields: Id, Name, Partner_Selected__c (Lookup to CVB_Partner__c)

Object: CVB_Partner__c
Fields: Id, Name

I want to do a query that gives me all CVB_Partner__c objects with the related Partner_Sent__c where the Partner_Selected__c is equal to the parent partner.

The code is much easier to understand:
Code:
Select Id, Name, (Select Id, Name, Opportunity__r.Partner_Selected__c from Partner_Sent__c where Opportunity__r.Partner_Selected__c = CVB_Partner__r.Id) from CVB_Partner__c

Unfortunately, I'm getting an error that states CVB_Partner__r.Id is not a valid field.

Am I doing something wrong or is this even possible?

Thanks!
 



Colin LoretzColin Loretz
I'm starting to think this is not possible using my current method and I may have to rearchitect both my controller and visualforce page to get this to work.

What I'm aiming for is a lot like the code below, but in a single query.

Code:
for(CVB_Partner__c partner : partners)
{

query = 'Select Name, (Select Name from Partners_Sent__c where Opportunity__r.Property_Selected__c = \'' + partner.Id + '\') from CVB_Partner__c where Id in (Select Property_Selected from Opportunity where StageName = 'Definite');

}

This is the ideal outcome:
 
Code:
 query = 'Select Name, (Select Name from Partners_Sent__c where Opportunity__r.Property_Selected__c = CVB_Partner__r.Id) from CVB_Partner__c where Id in (Select Property_Selected from Opportunity where StageName = 'Definite');

 But again, Salesforce tells me CVB_Partner__r.Id is not valid.

Can anyone provide some input on this?

Thanks!