[SalesForce] How to get all participants from Event
How get all participant from each Event and check if they are leads or not? Is it possible with one query?
Best Answer
This is how I would do it with single query:
Set<Id> leadIds = new Set<Id>();
for(EventRelation e : [SELECT RelationId FROM
EventRelation WHERE EventId = 'SOME_EVENT_ID' AND Status = 'Accepted' AND RelationId in (Select Id From Lead)])
{
leadIds.add(e.RelationId);
}
Also, just in case if you have a list of Event Ids in a variable called eventIds, you can rewrite the query to be like this:
[SELECT RelationId FROM
EventRelation WHERE EventId in :eventIds AND Status = 'Accepted' AND RelationId in (Select Id From Lead)]
For more info on EventRelation object check this link
EDIT
I'm adding here the 2nd approach that I would actually prefer, even though it's not a single query as OP requested:
Set<Id> participantIds = new Set<Id>();
for(EventRelation e : [SELECT RelationId FROM
EventRelation WHERE EventId = 'SOME_EVENT_ID' AND Status = 'Accepted'])
{
participantIds.add(e.RelationId);
}
List<Lead> leads = [SELECT Id, Name FROM Lead WHERE Id in :participantIds];
I prefer this second approach simply because I'm not sure how taxing it is to call the following part of the query in the first approach - RelationId in (Select Id From Lead).
The short answer is not easily, or at least not with a single query like you're showing above. If the WhatId is not associated directly with the account, you would need to query all related objects to the account, then the associated tasks. If this is in a trigger, making it bulk safe is going to involve a couple of maps.
The name of the field isn't CreateDate but CreatedDate.
The following query will, in example query all leads which were created in the current year:
SELECT id, firstname, lastname, company, CreatedDate
FROM Lead
WHERE CreatedDate = THIS_YEAR
/services/data/v29.0/query?q=SELECT+id,+firstname,+lastname,+company,+CreatedDate+FROM+Lead+WHERE+CreatedDate+=+THIS_YEAR
Best Answer
This is how I would do it with single query:
Also, just in case if you have a list of Event Ids in a variable called
eventIds
, you can rewrite the query to be like this:For more info on
EventRelation
object check this linkEDIT
I'm adding here the 2nd approach that I would actually prefer, even though it's not a single query as OP requested:
I prefer this second approach simply because I'm not sure how taxing it is to call the following part of the query in the first approach -
RelationId in (Select Id From Lead)
.