You need to sign in to do that
Don't have an account?
SOQL: limit parent records returned by child IDs
Hi there
I have two custom objects with a master-detail relationship, CustomProject__c and CustomPayment__c. CustomProject__c is the parent and can have many payments. I have a set of IDs of payments and want to find all the parents of these payments.
SELECT Id, Name, TotalPayments__c, (SELECT Id, Amount__c FROM CustomPayments__r WHERE Id IN :paymentIds) FROM CustomProject__c
I would expect this to give me just the CustomProject__c parents that have CustomPayment__c children with IDs in the list paymentIds. It doesn't. Instead I get *all* CustomProject__c parents. Can anyone shed any light on this?
Thanks,
Eliot Stock.
No, not true at all. Queries can look both directions.
All Answers
Why not just reverse it such that the CustomPayments__c is the main query and CustomProject__c is the subquery? That'll probably fix it.
No, not true at all. Queries can look both directions.
To your original question, the reason your query didn't work is that the main query won't scope itself to the subquery. Think about what your main query would be if that subquery weren't there:
SELECT Id, Name, TotalPayments__c FROM CustomProject__c
That would just return all CustomProject__c, which is in fact what it's doing. The WHERE in your subquery only scopes those subquery results. That's why you had to flip the query to get it to work.