質問

I'm running into some trouble filtering my SOQL Query by date where the date field belongs to the child of the object I'm querying.

I couldn't find anything in the documentation, and I tried two different queries, both of which failed:

SELECT Name, (SELECT Date__c FROM Child__r) 
FROM Parent Where Date__c >= <todays_date>

SELECT Name, (SELECT Date__c FROM Child__r) 
FROM Parent Where Child__r.Date__c >= <todays_date>

Is this even possible in Salesforce?

役に立ちましたか?

解決

Its not totally clear from your question exactly what your trying to filter. But if you want to get a list of parents that have a child record with a date matching some critera then you can use a semi-join, e.g.

select name from parent where 
      id in (select parentId from child where date__c > :today)

You can also add in the child sub query if you want the child data as well e.g.

select name, (select someChildFields from child__r) from parent where 
      id in (select parentId from child where date__c > :today)

This will get you the parent that have a child with the criteria, and for each parent, get all the children. You can also filter the sub-query on the same criteria if you only want the children that match the criteria, e.g.

select name, (select someChildFields from child__r where date__c > :today) 
      from parent where id in (select parentId from child where date__c > :today)

Finally, depending on exactly what you're trying to do, you can also flip it around and query the child table directly, pulling in data from the parent record, e.g.

select childFields, parent__r.name from child where date__C > :today
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top