You don't have to update the DurationInMinutes
field if you're also updating the EndDateTime
field. The system will populate it or update it automatically.
You just can't populate both unless the values match (i.e., the DurationInMinutes
value is actually the difference in minutes between StartDateTime
and EndDateTime
).
From the Event SOAP Reference:
In versions 13.0 and later, this field (DurationInMinutes
) is optional, depending on the following:
If IsAllDayEvent is true, you can supply a value for either DurationInMinutes or EndDateTime. Supplying values in both fields is allowed if the values add up to the same amount of time. If both fields are null, the duration defaults to one day.
If IsAllDayEvent is false, a value must be supplied for either DurationInMinutes or EndDateTime. Supplying values in both fields is allowed if the values add up to the same amount of time.
If you are making updates to an Event, you can either
- Not populate (not query) the
DurationInMinutes
field, in which case the system will autopopulate it;
- Null out the
DurationInMinutes
field (same result);
- Calculate the update to
DurationInMinutes
yourself.
Note that
Event e = [SELECT StartDateTime, EndDateTime, DurationInMinutes FROM Event LIMIT 1];
e.EndDateTime = e.EndDateTime.addMinutes(1);
update e;
results in an error, because we populate but do not update DurationInMinutes
.
This works fine, because we don't query DurationInMinutes
:
Event e = [SELECT StartDateTime, EndDateTime FROM Event LIMIT 1];
e.EndDateTime = e.EndDateTime.addMinutes(1);
update e;
as does this, where we set the field to null
:
Event e = [SELECT StartDateTime, EndDateTime, DurationInMinutes FROM Event LIMIT 1];
e.EndDateTime = e.EndDateTime.addMinutes(1);
e.DurationInMInutes = null;
update e;
and this also works fine, because we update the field manually.
Event e = [SELECT StartDateTime, EndDateTime, DurationInMinutes FROM Event LIMIT 1];
e.EndDateTime = e.EndDateTime.addMinutes(1);
e.DurationInMinutes += 1;
update e;
So the upshot is you have no need to calculate the field yourself. Just don't query it in the first place, if you're not using it, or set it to null
at the time you perform the update
DML operation and the system will calculate it for you.