• Becky McGrattan
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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.
  • January 26, 2020
  • Like
  • 0