6

It says that IsRecurrence2 and its related fields (Recurrence2PatternText, Recurrence2PatternVersion) are read only, but have to get set on initial event creation. When attempting to set them on a new event in Apex, it gives me Event.IsRecurrence2 is not writeable error.

Is it possible to set up an event series in Apex? Or does Salesforce only allow this from the front end?

1
  • Did you ever find a solution?
    – zaitsman
    Mar 15, 2023 at 4:01

2 Answers 2

1

Well it's ugly but it SEEMS to work:

Event e = new Event();
Map<String, Object> untypedEvent = (Map<String, Object>) json.deserializeUntyped(json.serialize(e));
untypedEvent.put('IsRecurrence2', true);
// put other fields such as pattern
e = (Event) json.deserialize(json.serialize(untypedEvent), Event.class);
system.debug(e.IsRecurrence2); // shows true

Hopefully someone has a better answer though.

Here is an answer I wrote some time back about working with untyped objects and using JSON to serialize/deserialize them.

My instinct is that there is some sort of constructor or method that can be used to set the field that is being hidden from us and is not well documented.

4
  • Thanks for something to try, but there is some unfortunate news... I tried this and then tried an insert with the event after deserializing and casting it back to the event and it inserted an event, but there was no recurrence. I checked the IsRecurrence2, Recurrence2PatternText, and Recurrence2PatterVersion (all fields I set in my testing). IsRecurrence2 was false in the database and the other two fields were blank. Like it didn't crash with read only errors but still just tossed them out :/ Jan 28, 2020 at 19:07
  • BOOOOO well, worth a try I suppose.
    – gNerb
    Jan 28, 2020 at 19:09
  • 1
    It's crazy that they wouldn't provide a way to do this, but I can't find any documentation on how. But honestly with Salesforce it wouldn't fully surprise me that they don't allow these set through code cuz Salesforce reasons. Jan 28, 2020 at 20:01
  • 1
    IsRecurrence2 is a read-only field, please refer to my post to see how you can use Recurrence2PatternText field to create create series event in Apex
    – Yogesh D
    Dec 20, 2023 at 22:21
0

You can create a lightning event series in Apex using the Recurrence2PatternText field. This field is available in API version 44.0 and later and has the Create property in API version 52.0 and later.

Here is a working example that creates 5 events in a series repeated daily,

Event event = new Event();
event.Subject = 'test';
event.Recurrence2PatternText = 'RRULE:FREQ=DAILY;INTERVAL=1;COUNT=5';
event.StartDateTime = DateTime.now();
event.EndDateTime = DateTime.now().addHours(1);
insert event;

Here is the detailed document that has more info on different series fields you can use for different purposes when dealing with series events.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .