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
Nirmal ChristopherNirmal Christopher 

unable to update certain fields via trigger in events

In a scenario i want to retain only the old values of the event if the event is updated directly when related to a custom object. Here is my  trigger. IT works fine for all the event fields except event start date and end date. These two field are not retaining their old values, It accepts the user Inputs.Also Debug statements working fine as expected. But its not updating the field values.

trigger prohibitUpdate on Event (before update) {
Set<id>neweveid=new set<id>();
Set<id>whatid=new set<id>();
for(event eve:trigger.new){
neweveid.add(eve.id);
whatid.add(eve.whatid);
}

for(event eve:trigger.new){

Event oldEve = Trigger.oldMap.get(eve.id);
if(trigger.isupdate){
system.debug('******'+trigger.old[0].StartDateTime);
          eve.StartDateTime=oldEve.StartDateTime;
          eve.EndDateTime=oldEve.EndDateTime;
          eve.Onsite_Arrival_DateTime__c = oldEve.Onsite_Arrival_DateTime__c;
          eve.Onsite_Completion_DateTime__c =  oldEve.Onsite_Completion_DateTime__c;
          eve.Location = oldEve.Location;
          eve.Description = oldEve.Description;
          eve.OwnerId = oldEve.OwnerId;
          eve.ShowAs = 'OutofOffice';
          eve.Subject = oldEve.Subject;
          eve.WhoId = oldEve.WhoId;       
}
}
}
Best Answer chosen by Nirmal Christopher
Nirmal ChristopherNirmal Christopher
If we dont use this this may never work consider checking these items below

Two additional Event fields had to be overwritten:

newEve.ActivityDateTime = oldEve.ActivityDateTime ;

newEve.DurationInMinutes= oldEve.DurationInMinutes;

Apparently there is logic associating these fields with the new StartDateTime and EndDateTime fields. If there is an inconsistency, then the "old" values are rejected and the "new" values are used.

All Answers

Nirmal ChristopherNirmal Christopher
Whats wrong with the start date and end date fields since other fields are working fine as expected
Nirmal ChristopherNirmal Christopher
If we dont use this this may never work consider checking these items below

Two additional Event fields had to be overwritten:

newEve.ActivityDateTime = oldEve.ActivityDateTime ;

newEve.DurationInMinutes= oldEve.DurationInMinutes;

Apparently there is logic associating these fields with the new StartDateTime and EndDateTime fields. If there is an inconsistency, then the "old" values are rejected and the "new" values are used.
This was selected as the best answer