1

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.

2
  • Provide us some code please Feb 16 '14 at 18:07
  • Added a sample code.
    – SFDC_MJ
    Feb 16 '14 at 18:20
1

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.

1
  • yeah.. that seems to be a good workaround approach.. Thanks Anil bhai..
    – SFDC_MJ
    Feb 19 '14 at 10:28
2

During the insert of the Event the EventRelation records won't exist yet.

They can't be inserted until after the Event is fully inserted as they have a reference to it via EventID.

AFAIK you can't create a trigger on EventRelation to detect the inserts.

Instead you could create a Time-Dependent Workflow on Event that will fire your Apex code once everything is setup and check for EventRelation records referencing inactive contacts. That, or a scheduled batch job to perform the check periodically.

4
  • In afterInsert Trigger for Event EventRelation record does exist for the Primary Contact but not for Secondary contact. Can you suggest a workaround for this.
    – SFDC_MJ
    Feb 17 '14 at 11:22
  • According to my requirement Check should not be performed periodically... Instead system should not allow user to Create an event if any of the contact is inactive.
    – SFDC_MJ
    Feb 17 '14 at 11:25
  • I can't think of a way to immediately enforce this constraint without a trigger on EventRelation. You could vote on the idea Allow Apex Triggers on Shared Activities -TaskRelation and EventRelation Objects Feb 17 '14 at 18:36
  • Thanks Daniel. You are right... its a salesforce limitation.
    – SFDC_MJ
    Feb 19 '14 at 10:25
1

I am hoping that order of execution is as below:

  1. Event record gets created.
  2. All the contacts then get attached to the event in the EventRelation junction table.
  3. Event workflows then fire which can make a field update on the event.

So if we are trying to get all the contact details in step 1, we are not getting as Daniel pointed out the eventid still does not exists. What if we do a dummy update in the event each time it gets created and the trigger would fire on the dummy update of the event and check the contact details. The contact details should now exists since the Event now has an ID created.

2
  • I tried this biut it still doesnt work. Secondary Contact Id's still not fetched.
    – SFDC_MJ
    Feb 17 '14 at 14:59
  • 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. Feb 18 '14 at 22:19

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.