Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

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 approaches:

  1. Simple - The exact related record(s) are already known. So for example the resource lookup has already been populated with the id of room one.

  2. Complex - The exact related record(s) still need to be selected after this method has been called. So for example 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, so the user (or other process) can select which resource and/or time slot is best suited.

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

global static B25.TimeSlotFinder.Result findTimeSlots(B25.TimeSlotFinder.Context)

Parameters

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

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

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

Parameters

List<B25.TimeSlotFinder.Context>

A list of request contexts.

Return Type

B25.TimeSlotFinder.Result

A list of results.

Inner Classes

 Context

Description

This class wraps the request context.

Properties

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

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.

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.

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.

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

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:

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:

Reservation__c reservation

The actual reservation record.

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).

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:

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);
}
  • No labels