i wrote a function inside event trigger handler that should receive custom times for a meeting, a custom time zone of the customer, and a timezone for the user - and calculate the time of the meeting according to the user's timezone.
for example:
Customer_Time_Zone__c : GMT -5
User's Time Zone: GMT + 2
Start_Customer_Time_Zone__c : 10:00am
End_Customer_Time_Zone__c: 11:00am
The function will receive this data and calculate that the standard time of the meeting is 17:00pm-18:00 in the user's timezone, and fill the event standard time fields StartDateTime, EndDateTime.
The problem is: i can't do that, because the fields are read only. i have no idea how to update these fields, and if there are more fields that are related to them such as duration etc.
Here is the function so far:
private static void updateStandartDateTimeFields(User u, Event e)
{
System.debug('updateStandartDateTimeFields start');
System.debug('User: ' + u);
System.debug('Event: ' + e);
e.StartDateTime = e.Start_Customer_Time_Zone__c;
e.EndDateTime = e.End_Customer_Time_Zone__c;
Update e;
}
When I run a test I receive the following error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, EventTrigger: execution of AfterInsert
caused by: System.FinalException: Record is read-onlyClass.EventTriggerHandler.updateStandartDateTimeFields: line 131, column 1
Class.EventTriggerHandler.createMeeting: line 59, column 1
Class.EventTriggerHandler.afterInsert: line 31, column 1
Class.TriggerHandler.run: line 45, column 1
Trigger.EventTrigger: line 13, column 1: []
Can I avoid this error? If so, how?
after insert
andafter update
events. Why don't you perform the update onbefore
events?