You can try to query the junction object like this:
Select ObjectC__r.Id, ObjectC__r.Name
From ObjectA__c
Where ObjectC__r.Date__c >= ObjectB__r.start_date__c
And ObjectC__r.Date__c <= ObjectB__r.end_date__c
There's really no need to use table names in your query (e.g. select account.id from account
), but if you are insisting on using them, then your second query probably should have been:
SELECT Account__c, AccountLocations__c.Account__r.ID FROM AccountLocation__c
Use __r only to refer to a relationship. The table name doesn't change to __r. This is one reason I tend to avoid table names/aliases in straight queries, because it complicates otherwise easy code.
As a side note, Account__c and Account__r.Id refer to the same value. You really wouldn't normally refer to both of them that way.
The second query wouldn't return that form, although the first would (I presume this what you meant). Extracting the values can be done several ways. One possible way to get just Id values could look like:
Set<Id> ids = new Map<Id, AccountLocation__c>(results[0].AccountLocation__r).keySet();
However, depending on your use case, you might not even need to do that. You can bind an SObject list to a query variable, for example, and the query returns all values that match the IDs in the list.
You could also do this a bit more manually and just loop through the values and add them to a list or set.
Best Answer
For simplicity I will rename your objects to:
X
=>Detail
Y
=>Parent
Z
=>Child
The most reasonable interpretation of your OP I can infer is:
If this interpretation is correct, you can do the following:
Another interpretation I could see is:
If this interpretation is correct, I would add a field on
Parent
along the lines ofHas_Child_Records__c
, which you can manage fromProcess Builder
. Just set it up onChild
to update theParent
checkbox with the criteriaOR(ISNEW(), ISCHANGED(Parent__c))
. Then you can just do:If neither of these interpretations is correct, please add more detail to your question to clarify.