2

How can I get a list of Contacts which ahve at least 1 Event created for them in a specified time period?

select Id, Name from Contact where  ID IN (Select WhoID from Event where ActivityDate >= 2013-12-29 and ActivityDate <= 2014-02-02)

I'm trying to execute this query (above), but it is throwing error :

...Entity 'Event' is not supported for semi joins...

2 Answers 2

1

You've got the right idea, but unfortunately the lookups on events can't be used in these queries because of the way they can work with multiple objects (unlike regular lookups).

What you'll have to do here is do two queries, one on events where you loop over the results and put WhoId into a set of IDs, and then one on contact where you use where Id in : theSet:

Set<Id> contactIds = new Set<Id>();

for(Event e : [Select WhoID from Event where ActivityDate >= 2013-12-29 and ActivityDate <= 2014-02-02])
{
  contactIds.Add(e.WhoID);
}

List<Contact> contacts = [select Id, Name from Contact where ID IN : contactIds];
2
  • thnx for the suggestion and this is exactly what I've been doing :-) ... but I believe this is just a waste of an SOQL :(
    – VarunC
    Commented Feb 25, 2014 at 15:20
  • It's a waste in terms of the limits but it's unavoidable unfortunately. OTOH it's not often where you do hit that particular limit!
    – Matt Lacey
    Commented Feb 25, 2014 at 21:40
5
select Id, Name from Contact 
where ID IN (SELECT RelationId from EventRelation 
            where CreatedDate >= 2013-12-29T00:00:00Z 
            and CreatedDate <= 2014-02-02T00:00:00Z AND  Relation.Type='Contact')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .