I am trying to update only newly added attendees from Event to event relations. Event attendees initials are tied to the user profile.

I get an error at this line : e.RelationId = attendeeId; Erro name : AfterUpdate caused by: System.SObjectException: Field is not writeable EventRelation.RelationId

This is a follow-up question on How to only update for new multi select pick list values after update trigger?

Thanks in advanced.

trigger cloneEventTrigger on Event (after insert, after update) {

if(trigger.isUpdate) {        
   cloneEventHelperClass.onAfterUpdate(trigger.new, trigger.oldmap);



   public static void onAfterUpdate(List<Event> newEvnt, Map<Id, Event>oldMap){  
    // get all fields from Events
    List<Id>eventIds = new List<Id>();
    for(Event evnt : [SELECT Id From Event WHERE id=:newEvnt AND Attendees__c != NULL])
            if (evnt.Id != null)
                System.debug('EventIDs' + eventIds);

    Set<String> oldInitials = new Set<String>();
    for (Event e : newEvnt)
        if (e.Attendees__c != null)

            system.debug('oldEven: ' + oldMap.get(e.Id).Attendees__c);
            String multiValues = oldMap.get(e.Id).Attendees__c;
            splitList= multiValues.split(';');
    Event[] queryEvent = [SELECT Id, Attendees__c From Event WHERE id=:eventIds];

    Map<String, Id> initialsToUserId = new Map<String, Id>();
    Set<String> newInitials = new Set<String>();

    Set<String> initials = new Set<String>(); //created to hold only new initials
    Map<id,User> updateAll= new Map<id, User>();

   for(Event record: queryEvent) {

   // Find only newly added Initials
    for(String init: newInitials){
    List<String>newInitialstoAdd = new List<String>();

// query for user and their initials and add them to Map. 
    for(User record: [SELECT Initials__c FROM User WHERE Initials__c = :initials AND IsActive = TRUE]) {
      initialsToUserId.put(record.Initials__c, record.Id);

     //loop through existing Eventrelation ID and Insert only new ID
    list <EventRelation> upRelations =[select id, EventId,RelationId, Event.OwnerId from EventRelation where EventId IN: eventIds];
    list <EventRelation> upRelationsIns = new list <EventRelation>();
    System.debug('upRelations' + upRelations);
    for(EventRelation record: upRelations) {
        for(String initial: initials) {
                Id attendeeId = initialsToUserId.get(initial);                
                if(attendeeId != null) {
                     EventRelation e = new EventRelation();
                     e.id= record.Id;
                     e.RelationId = attendeeId;




       insert upRelationsIns; 
       System.debug('Only NEW ID was inserted');

    } //End of onAfterInsert Trigger

1 Answer 1


Notice that RelationId field on the EventRelation is not updatable and hence you are getting the errors.

What you can do is instead of updating it, create for new ones and for older records delete and recreate it.

Also from your trigger code it looks like you are still learning, Recommend moving logic to an apex class and call your apex class from triggers.

  • 1
    Thanks, Mohith. I will try removing older Event Relations and inserting all over again with new IDs for Event Relations.
    – mike
    Apr 20, 2020 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .