In Winter 09 support was added for semi/anti joins between objects. What this allows you to do is write a SOQL relationship query that returns just records that have a parent/child record or just records that don't have a parent/child record. Essentially an inner join in SQL. Unfortunately, this does not work with polymorphic keys. So you can't write a query that returns just the custom objects that have attachments. The following query as it is today will function as an outerjoin thus returning all of the custom objects for the specified account whether they have an attachment or not.
Select c.name, c.id, c.recordtype.name, (select a.name, a.createddate from attachments a)
from custom_object__c c
where c.accountid = 'xxxxxxxxxxxxxxx'
order by c.createddate desc
The Winter 09 functionality would make the query look like this but currently it doesn't work with the polymorphic relationship between attachments and their parent object.
Select c.name, c.id, c.recordtype.name, (select a.name, a.createddate from attachments a)
from custom_object__c c
where c.accountid = 'xxxxxxxxxxxxxxx'
and c.id in (select parentid from attachment)
order by c.createddate desc
Comment » Posted by joshvh
Posted 11/14/08
Categories: Web Services API, Apex and Visualforce, Force.com Platform