[SalesForce] Fetch Secondary contact id while creating an event with multiple contacts

I am creating an Event with Multiple contacts. I want to display an error message if the status of any related contact is not active. I am unable to fetch Secondary Contact id's even through EventRelation object, on Insert of an event. On update, event relation is returning me all the id's but not on insert. Can you please help.

Thanks in advance.

Here is a sample code:

This is a function which runs on after insert of an event :

public static void Example(List<Event> listEvent){

    List<Id> ListContactId = new List<Id>();
    Set<Id> SetErrorEvent = new Set<Id>();
    Set<Id> ErrorEvent = new Set<Id>();
    String Active = System.Label.Contact_Status_Active;
    List<EventRelation> ListEventRelation = new List<EventRelation>();
    ListEventRelation = [Select RelationId,EventId from EventRelation where 
                         EventId in: listEvent AND isParent = true AND isWhat = false];
    for(EventRelation er:ListEventRelation){ 

        ListContactId.add(er.RelationId);
    }

    for(Contact c: [Select id, Contact_Status__c from Contact where id in :ListContactId  
                    and Contact_Status__c !=: Active]){
        ErrorEvent.add(c.id);
    }
    system.debug('!!! :'+ErrorEvent);
}   

ErrorEvent is returning only the Primary Contact id but not Secondary contact id's.

Best Answer

If eventrelation table gets populated only after all the event workflows are fired, then i am afraid there is no way to query all contact details in real time. But as a workaround what you could do is:

  1. Create a validation rule on event object that you cannot add contact attendee's during event creation (i.e on insert, you can use the ISNEW() function in the validation rule). So the event gets created first with no attendees at this point.
  2. Now the users could add the attendees to the event by editing the event and clicking the add attendee button.

I know this is a kind of workaround, but this would avoid creating a VF page just to add attendees.