Skip to main content
Skip table of contents

TimeSlotFinder

Finds available time slots for a given reservation.

For each dimension that a reservation can be related to (through either a lookup or a junction), this class supports two use cases:

Simple - The exact related record is already known. I.e. the resource lookup has already been populated with the id of room one.

Complex - The related record can be any out of a pool of records. I.e. we have a pool of resources (let’s say all rooms in building one), for which we want to know all the possible time slots.

These use cases can be mixed in a single call to the method. For example the resource is fixed but the staff can be any from a pool of staff members.

Example use cases for lookups

Simple use case

The reservation I want to make has to be in room one, with staff Alex. I pass this reservation to the method, with the resource and staff lookups populated. The method result gives me a simple list of time slots when both the room and the staff are available.

Complex use case

The reservation I want to make has to be in building one (so in any of the rooms there), with any of the staff that work at building one. I pass the reservation to the method, without the resource and staff lookups populated. I separately pass the allowed room ids and staff ids. The method result gives me a more complex list of time slots, each with all the possible room and staff combinations available at that time.

We have some handy utilities for finding the ids to pass to this method!

To get all the room ids in a specific building, use the DimensionHierarchy global class.

To get all the staff that work in building one (defined through availability records), use the MdaLinkedDimensions global class.

Example use cases for junctions

Simple use case

The reservation I want to make has to have two reservation contacts: John and Alex. I pass this reservation to the method, with two reservation contact records (one for John, one for Alex) in the separate junctions property. The method result gives me a simple list of time slots when both John and Alex are available.

Complex use case

The reservation I want to make has to have two participants (reservation contacts) out of five possible participants. I pass five reservation contact records (one for each possible participant) in the junctions property. In the requestedNumberOfJunctions property, I specify that I need two reservation contacts to be available. The method result gives me a complex list of time slots, each with all the possible combinations of participants available at that time.

Methods

findTimeSlots
Description

This method returns the times that a given reservation (optionally with junctions) is possible.

Signature
CODE
global static B25.TimeSlotFinder.Result findTimeSlots(B25.TimeSlotFinder.Context)
Parameters
CODE
B25.TimeSlotFinder.Context

Contains the reservation, its junctions, and a definition of what the resulting series of time slots should look like (such as slot duration and interval ).

Return Type
CODE
B25.TimeSlotFinder.Result

Contains a List<B25.TimeSlot> timeSlots property with the available time slots.

findTimeSlotsBulkified
Description

Bulkified version of findTimeSlots. At the time of writing, this method is for convenience only and does not do anything more efficient than calling the unbulkified method multiple times.

Signature
CODE
global static List<B25.TimeSlotFinder.Result> findTimeSlotsBulkified(List<B25.TimeSlotFinder.Context> contexts)
Parameters
CODE
List<B25.TimeSlotFinder.Context>

A list of request contexts.

Return Type
CODE
B25.TimeSlotFinder.Result

A list of results.

Inner Classes

Context
Description

This class wraps the request context.

Properties
CODE
B25.TimeSlotGenerator.Context timeSlotContext

Contains the definition of what the resulting series of slots should look like, such as the date range, slot duration and slot interval. For more details see TimeSlotGenerator

CODE
B25__Reservation__c reservation

The reservation to find available time slots for. Make sure all fields that can influence conflict checking are populated. If you are applying the simple use case for lookups (explained here), make sure any lookups to dimensions (resource/staff/etc) that need to be available are populated. Start and end times are not necessary and will be ignored.

CODE
Map<String, List<Id>> fieldIds

Optional. If you are applying the complex use case for lookups, populate this with the possible ids mapped by the api name of the corresponding lookup field.

CODE
Map<String, List<SObject>> junctions

Optional. If you need any junctions to be available, add them here mapped by relationship name. Applies to both the simple and the complex use case.

CODE
Map<String, Integer> requestedNumberOfJunctions

Optional. If you are applying the complex use case for junctions, populate this with the required numbers of available junctions, mapped by relationship name.

Result
Description

This class wraps the result, which contains a list of available time slots.

Properties
CODE
List<B25.TimeSlot> timeSlots

The time slots when the given reservation (and junctions) are available. Each time slot has a startDateTime and endDateTime.

If you are applying the complex use case for at least one lookup or junction, this property will contain additional data. Each B25.TimeSlot entry in this list will be castable to B25.TimeSlotWithReservations, which have an additional property:

CODE
List<B25.Reservation> reservations

This property will contain all the reservations that are possible during that time slot. The B25.Reservation data type is a wrapper around a reservation and its child records. It has the following properties:

CODE
Reservation__c reservation

The actual reservation record.

CODE
Map<String, List<SObject>> childRecords

The child records of the reservation (mapped by relationship name). When applying the complex use case for junctions, this will contain the available junctions at the given time slot.

Example

Simple use case

This example shows the simple use case (both for lookups and junctions).

CODE
B25.TimeSlotFinder.Context context = new B25.TimeSlotFinder.Context();

// set the reservation to find time slots for
context.reservation = new B25__Reservation__c(
    // make sure to populate all fields that are relevant for conflict checking
    // and link this reservation to all dimensions that need to be available
    B25__Resource__c = someResourceId
    // note that start and end times are not necessary
);

// if you need any junctions to be available, map them by relationship name
context.junctions = new Map<String, List<SObject>>{
    'B25__Reservation_Contacts__r' => new List<SObject>{
        new B25__ReservationContact__c(
            B25__Contact__c = someContactId
            // note that a reservation id is not necessary
        )
    }
};

// the time slot context defines what the resulting series of slots should look like
context.timeSlotContext = new B25.TimeSlotGenerator.Context();
context.timeSlotContext.startOfRange = System.now();
context.timeSlotContext.endOfRange = System.now().addDays(7);
context.timeSlotContext.duration = 60;
context.timeSlotContext.interval = 15;

// call the method and do something with the result
B25.TimeSlotFinder.Result result = B25.TimeSlotFinder.findTimeSlots(context);
for (B25.TimeSlot timeSlot : result.timeSlots) {
    System.debug('available time slot from ' + timeSlot.startDatetime
        + ' until ' + timeSlot.endDatetime);
}

Complex use case

This example shows the complex use case for lookups:

CODE
B25.TimeSlotFinder.Context context = new B25.TimeSlotFinder.Context();

// the time slot context defines what the resulting series of slots should look like
context.timeSlotContext = new B25.TimeSlotGenerator.Context();
context.timeSlotContext.startOfRange = System.now();
context.timeSlotContext.endOfRange = System.now().addHours(2);
context.timeSlotContext.duration = 60;
context.timeSlotContext.interval = 15;
// specify all the potential resource ids
// NOTE: replace these with resource ids that exist in your org!
context.fieldIds = new Map<String, List<Id>>{
	'B25__Resource__c' => new List<Id>{'a0eJX000000hnL7YAI', 'a0eJX000000hnL8YAI'}
};

// call the method and do something with the result
B25.TimeSlotFinder.Result result = B25.TimeSlotFinder.findTimeSlots(context);
for (B25.TimeSlot timeSlot : result.timeSlots) {
	System.debug('available time slot from ' + timeSlot.startDatetime
		+ ' until ' + timeSlot.endDatetime);
	B25.TimeSlotWithReservations timeSlotWithReservations = (B25.TimeSlotWithReservations) timeSlot;
	System.debug(timeSlotWithReservations.reservations);
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.