[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).

Related Topic