function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joel RJoel R 

Issue changing Contact to Invitee on EventRelation

Hi Everyone,

I've spent quite a while trying to resolve my issue with no success so reaching out to the "brains trust".

I want to be able to update the Event's related contact (i.e. WhoId) to be an Invitee for the event when a custom "Send Invite to Contact" checkbox field is checked.

I have created an Event trigger (after insert, after update), so that when a new Event is created or an existing record is updated and the new field is true, the trigger looks for the related EventRelation records for the Event, for the WhoId (EventRelation.RelationId), and set the IsAttendee field to TRUE.

Below is the snippet of code in the Trigger:
List <EventRelation> ers = [
            SELECT Id, EventId, RelationId, IsParent, IsInvitee, Status
            FROM EventRelation
            WHERE EventId IN : evtsForCalInvites.keySet()
];
// event Id / contact id, EventRelation
Map <string, EventRelation> ersMap = new Map <string, EventRelation>();
for (EventRelation er : ers)
{
    ersMap.put((string)er.EventId + (string)er.RelationId, er);
}

List <EventRelation> ersToUpsert = new List <EventRelation> ();
for (Event evt : evtsForCalInvites.values())
{
    EventRelation er = new EventRelation();            
    if (ersMap.containsKey((string)evt.Id + (string)evt.WhoId))
    {                
        System.debug('Existing EventRelation record found');
        er = ersMap.get((string)evt.Id + (string)evt.WhoId);
    }
    else
    {
        System.debug('Creating new EventRelation record');
        er.EventId = evt.Id;
        er.RelationId = evt.WhoId;
    }            
    er.IsInvitee = true;
    ersToUpsert.add(er);
}
upsert ersToUpsert;

I created a Test case that performs the following:
  1. Create Contact
  2. Create Event with Contact as WhoId and set Send_Calendar_Invite_to_Contact__c = true to the code in the trigger fires.
By the time the after trigger has fired, the EventRelation records are already created, and so the EventRelation record will always be updated.

When the upsert occurs, I get the following error message from Salesforce "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, When isInvitee is false, the invitee attributes Status, Response, and Responded Date may not be modified: []"

This is a very strange error message, which I haven't seen on the Internet before. Would someone be able to help?

Many thanks.
Watts JonasWatts Jonas
This derived object is a filtered version of the eventrelation object YourTexasBenefits (https://www.yourtexasbenefits.me/).
Joel RJoel R
Hi Watta Jonas,

Thanks for your reply. I'm not exactly sure what you mean. Are you saying that the SOQL query for EventRelation does not access the true EventRelation object?

The strange thing is that if I manually execute the following code in the Developer Console, the EventReletion record is created.
Contact c = new Contact()
c.Email = 'email@test.com';
c.FirstName = 'Test';
c.LastName = 'Contact';
insert c;

Event evt = new Event();
evt.WhoId = c.Id;
evt.Subject = 'Test Event';
evt.StartDateTime = DateTime.newInstance(System.today().year(), System.today().month(), System.today().day(), 13, 0, 0);
evt.EndDateTime = DateTime.newInstance(System.today().year(), System.today().month(), System.today().day(), 14, 0, 0);
insert evt;   

system.debug(evt.Id);

EventRelation er = [
    SELECT Id, IsInvitee 
    FROM EventRelation 
    WHERE eventId = : evt.Id 
    AND RelationId = : evt.WhoId
];
er.isinvitee = true;
update er;

But when I do the same from the "after" trigger of the Event object, I get the error "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, When isInvitee is false, the invitee attributes Status, Response, and Responded Date may not be modified: []"
Mani PMani P
Hi Joel,

Did you find any solution to this? We are also facing a similar kind of issue. Any suggestions would really help us.
Joel RJoel R
Hi Mani,

Unfortunately not, the issue is still unresolved. 
Becky McGrattanBecky McGrattan
Hello - did anyone get any further with this?
Joel RJoel R

Hi Becky, 

I haven't looked into this recently, but last time I exhaustively read up on similar issues, it looked like Salesforce doesn't support this in its implementation of EventRelation.
I ended up educating users, informing them that they also need to add the contact to the "Attendees" field when Events are created.
 

Hopefully that helps.

Moni JaisMoni Jais
I was facing the same issue and found the reason.
Please see the debug logs for this. Any flow(Process builder) must be trying to update the Event record at the same time. For my case, it was Activity chatter feed post.