1

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);
    }  

 } 
}

Class

   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)
            {
                eventIds.add(evnt.Id);
                System.debug('EventIDs' + eventIds);
            }          
        }

    Set<String> oldInitials = new Set<String>();
    List<String>splitList;
    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;
            If(!String.isEmpty(multiValues)){
            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) {
        newInitials.addAll(record.Attendees__c.split(';'));
    }  

   // Find only newly added Initials
    If(!splitList.isempty()){
    for(String init: newInitials){
        if(!splitList.contains(init)){ 
               initials.add(init);
        }      
      } 
    }
    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;

                     upRelationsIns.add(e);  

             }

        }
      }

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

    } //End of onAfterInsert Trigger
3

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.

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

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.