Shared Activities is enabled in Salesforce. Shared contacts (i.e. AccountContactRelation) is also enabled.
I need to return all Events where Contacts for an Account are invited. A Contact unrelated to the Account of interest may be the primary WhoId for the Event, so it is important to check against the additional invitees to see if the Account had invitee(s). I cannot use Event.AccountId as a filter because Salesforce uses the AccountId of the primary contact if the organization uses Shared Activities. If I invite contacts from Account A and Account B to the same event, the event should count when I view Account A and when I view Account B.
FYI - My_Market__c is a formula that evaluates to true if the market on my user matches the My_Market__c on the Event. Please help me write my query. What should it be?
Psuedo code:
public static integer getMeetingVelocity(String recordId) {
Event[] mtgVel = [SELECT My_Market__c, ActivityDate
(SELECT RelationId FROM EventRelation
WHERE Relation.Type = 'Contact' AND Relation.AccountId = :recordId)
FROM Event
WHERE My_Market__c = true AND ActivityDate >= LAST_N_DAYS:28
];
velocity = mtgVel.size();
return velocity;
}