For a certain business use case, I need to set the End datetime of an Event to 25 seconds from its Start datetime. Through the below apex code, I have noticed that the system does not actually set the End datetime unless it is at least a minute from the Start datetime. Anyone know a way to do so?

Datetime startTime = datetime.Now();
Datetime endTime = startTime.addSeconds(25);

system.debug('------- local apex variables ------');

Event rec = new Event(
Subject = 'Test event',
StartDateTime = startTime,
EndDateTime = endTime);
insert rec;

rec = [SELECT StartDateTime, EndDateTime, DurationInMinutes FROM Event WHERE Id =:rec.Id];

system.debug('------- values on the record ------');

The debug log showed the following entries

09:40:40.9 (9235598)|EXECUTION_STARTED
09:40:40.9 (9239769)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
09:40:40.9 (9722311)|USER_DEBUG|[4]|DEBUG|------- local apex variables ------
09:40:40.9 (9741026)|USER_DEBUG|[5]|DEBUG|2017-09-13 16:40:40
09:40:40.9 (9747653)|USER_DEBUG|[6]|DEBUG|2017-09-13 16:41:05
09:40:40.9 (42598303)|USER_DEBUG|[16]|DEBUG|------- values on the record ------
09:40:40.9 (42637322)|USER_DEBUG|[17]|DEBUG|2017-09-13 16:40:40
09:40:40.9 (42657167)|USER_DEBUG|[18]|DEBUG|2017-09-13 16:40:40
09:40:40.9 (44089534)|CODE_UNIT_FINISHED|execute_anonymous_apex
09:40:40.9 (44827761)|EXECUTION_FINISHED

As you can see the End datetime on the Event record is 2017-09-13 16:40:40 and not 2017-09-13 16:41:05. Any way, I can ensure the End datetime can be accurately updated?

  • 2
    I think the duration has to be at least one minute...
    – Adrian Larson
    Sep 13 '17 at 16:48

Unfortunately, it has to be at least a minute. EndDateTime is tied to DurationInMinutes, which is defined as having no decimal component (it's a Number(8,0)). For example, an event can't be 3 minutes and 2 seconds long. You'll need to come up with some other solution that doesn't involve 25 second long events.

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.