0

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;
}
1

Figured it out, so hopefully this helps someone.

public static integer getMeetingVelocity(String recordId){

List<AccountContactRelation> acrs = [SELECT Id, ContactId
                                     FROM AccountContactRelation
                                     WHERE Accountid = :recordId];

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

for(AccountContactRelation acr: acrs) contactIds.add(acr.ContactId);

List<EventRelation> ers = [SELECT id, EventId
                           FROM EventRelation
                           WHERE Relation.Type='Contact'
                           AND RelationId IN :contactIds];

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

for(EventRelation er: ers) eventIds.add(er.eventId);

return [SELECT Id, My_Market__c
        FROM Event 
        WHERE My_Market__c = true
        AND ActivityDate >= last_n_days:28 AND Id IN :eventIds].size();

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.