getOccurrencesByID in actionBegin event

Hi team,

I want to ensure that no collisions of appointments happen - also with repeated events.

If I create a new event, I use the onActionBegin event to check if there is already an appointment (or a recurring appointment with getOccurrencesByID(existingAppointment.Id). ​If there is one, I use args.cancel and do not allow to save the appointment.​

But if the new appointment is recurring, then the getOccurrencesByID returns an empty array. 

1) How can I prevent this? 


Also, when I delete the first occurrence of a repeating event (its added to the RecurrenceException property) it is still returned from the getOccurrencesByID  ​method. 2) how can I check if that particular occurrence is not deleted as exception? 


If I delete the very last occurrence of an appointment, I want to delete the entire appointment. 3) Is there a built-in way for this? 


Thanks,
Max




1 Reply 1 reply marked as answer

SR Swathi Ravi Syncfusion Team September 11, 2024 12:07 PM UTC

Hi Max,

Thank you for reaching out and for your detailed questions. We're happy to assist you with your requirements related to preventing appointment collisions, managing recurrence exceptions, and handling the deletion of recurring events. Below are our responses to each of your queries:

1. Preventing appointment collisions, including recurring events

To prevent appointment overlaps, you can use the actionBegin event in combination with the generateOccurrence method from the Schedule component. This approach will allow you to check for existing appointments (including recurring ones) and prevent saving new appointments if a time conflict is detected.

Here's an example implementation:

import { extend, isNullOrUndefined } from '@syncfusion/ej2-base';

export class AppComponent {
  public data: Record<string, any>[] = [
    {
      Id: 1,
      Subject: 'Project demo meeting with Andrew',
      Location: 'Office',
      StartTime: new Date(2021, 1, 14, 12, 0),
      EndTime: new Date(2021, 1, 14, 13, 0),
      RecurrenceRule: 'FREQ=DAILY;INTERVAL=1;COUNT=5',
      CategoryColor: '#1aaa55'
    },];
  @ViewChild('scheduleObj') public scheduleObj: ScheduleComponent;
  public onActionBegin(args: ActionEventArgs) {
    if (args.requestType === "eventCreate" || args.requestType === "eventChange") {
      let data = args.data instanceof Array ? args.data[0] : args.data;
      let appointementData = this.scheduleObj.eventsData;
      let appointementCollection = [];
      for (var i = 0; i < appointementData.length; i++) {
        if (!isNullOrUndefined(appointementData[i].RecurrenceRule)) {
          let occurrence = this.scheduleObj.eventBase.generateOccurrence(
            appointementData[i]
          );
          appointementCollection = appointementCollection.concat(occurrence);
        } else {
          appointementCollection.push(appointementData[i]);
        }
      }
      if (!isNullOrUndefined(data.RecurrenceRule)) {
        let occurrences = this.scheduleObj.eventBase.generateOccurrence(data);
        for (let i = 0; i < occurrences.length; i++) {
          // Get the timeslot and check if there is already an event
          let filteredEvents = this.scheduleObj.eventBase.filterEventsByRange(
            appointementCollection,
            occurrences[i].StartTime,
            occurrences[i].EndTime
          );
          if (filteredEvents.length > 0) {
            args.cancel = true;
            break;
          }
        }
      } else {
        args.cancel = !this.scheduleObj.isSlotAvailable(data);
      }
    }
  }
}


2: when I delete the first occurrence of a repeating event (its added to the RecurrenceException property) it is still returned from the getOccurrencesByID ​method. 

Regarding your issue with getOccurrencesByID returning deleted occurrences, we could not reproduce this behavior. When you delete an occurrence from a recurring series, it should not be returned by the getOccurrencesByID method. Please review the below shared sample that demonstrates the expected behavior.


3: If I delete the very last occurrence of an appointment, I want to delete the entire appointment. Is there a built-in way for this? 

There is no built-in support to automatically delete the entire series when the last occurrence is deleted.

We hope this addresses your concerns. Please let us know if you need further clarification or assistance. We’re committed to ensuring everything works smoothly for you.


Regards,
Swathi

Marked as answer
Loader.
Up arrow icon