Newer Version Available

This content describes an older version of this product. View Latest

AppointmentBookingService Class

Represents the appointment booking scheduling process in field service. Appointment booking returns the available slots for a service appointment, while considering scheduling policies, work rules, and service objectives. For example, we can schedule an appointment tomorrow between 9 and 11 AM, or next Monday between 4 and 6 PM.

Namespace

FSL

Usage

Before calling the AppointmentBookingService class, make sure that the parent work order and service appointment already exist. The operatingHoursId that you send represents the variation of slots offered to the customer during appointment booking. (For example, Mondays 9–11 AM, 11 AM–1 PM, 1–3 PM; Tuesdays 9–11 AM, 12–4 PM; and so forth.)

The time zone is typically based on the location of the customer requesting the service. As a best practice, use the time zone specified on the service territory’s operating hours.

When the AppointmentBookingService class is called, the ExactAppointment value on the associated work type is ignored. If exact appointments are needed, set this value to true in the parameters. If you want to respect the ExactAppointment of the work type, query it directly from the work type.

After receiving the slots, the developer can decide how to display, or manage, the slots. Typically, the slots are displayed to the customer, who selects a slot. After a slot is selected, the service appointment’s ArrivalWindowStartTime and ArrivalWindowEndTime fields are updated with the slot’s start and end times. The FSL.ScheduleService method is called so that the service appointment is scheduled correctly within the selected slot.

AppointmentBookingService Methods

AppointmentBookingService includes the following static method.

getSlots(serviceID, policyId, operatingHoursId, tz, exactAppointment)

Returns a list of FSL.AppointmentBookingSlot records.

Signature

public static List<FSL.AppointmentBookingSlot> getSlots(Id serviceID, Id policyId, Id operatingHoursId, System.TimeZone tz, Boolean exactAppointment)

Parameters

serviceID
Type: Id
Required. The ID of the service appointment being scheduled.
policyId
Type: Id
Required. The ID of the scheduling policy being used.
operatingHoursId
Type: Id

Required. The ID of the operating hours record used to determine time slot intervals.

The TimeZone on the operating hours record is ignored.

Note

tz
Type: System.TimeZone
Required. The time zone in which the slots are returned. This is normally the time zone of the service territory in which the service appointment is performed—in other words, the customer’s location. Appointment booking slots must be displayed in the time zone of the customer’s location.
exactAppointment
Type: Boolean
Required. Specifies whether the result uses exact appointments or an arrival window. When the getSlots() method is called, the exactAppointment value on the work type is ignored.

Return Value

Type: List<FSL.AppointmentBookingSlot>

Usage

This method returns a list of appointment booking time slots available for a given service appointment. Results are returned in the time zone sent in the original request. The appointment windows (9–11, 11–1, 1–3, and so on) depend on the operatingHoursId value provided in the call.

This method only returns available slots and does not schedule the service appointment. After the end user selects the desired slot, update the ArrivalWindowStartTime and ArrivalWindowEndTime properties on the service appointment and call the FSL.ScheduleService method.

Perform a time zone conversion to convert dateTime values to UTC before they are updated in the database.

Example

This example calls the AppointmentBookingService class using the Customer First scheduling policy and the Gold Appointments Calendar operating hours as the desired appointment slots.

1// FSL.AppointmentBookingService
2// The GetSlots method returns a list of AppointmentBookingSlot objects.
3
4ServiceAppointment sa = [SELECT Id, EarliestStartTime, DueDate FROM ServiceAppointment WHERE Id='Service Appointment ID'];
5Id schedulingPolicyId=[SELECT Id FROM FSL__Scheduling_Policy__c WHERE Name='Customer first' LIMIT 1].Id;
6Id operatingHoursId=[SELECT id FROM OperatingHours WHERE name='Gold Appointments Calendar' LIMIT 1].Id;
7Timezone tz = UserInfo.getTimeZone();
8
9List<FSL.AppointmentBookingSlot> slots = FSL.AppointmentBookingService.GetSlots(sa.Id, schedulingPolicyId, operatingHoursId, tz, false);
10
11System.debug('Returned ' + slots.size() + ' appointment slots');
12for(integer i=0; i<slots.size(); i++){
13    system.debug('Slot:'+i+' Start: '+slots[i].Interval.Start+' Finish:'+ slots[i].Interval.Finish+' Grade: '+slots[i].Grade);
14}

After a slot is selected, the service appointment is updated as follows.

1FSL.AppointmentBookingSlot slot = slots[0];
2sa.ArrivalWindowStartTime = slot.Interval.Start.addSeconds(tz.getOffset(slot.Interval.Start) / -1000);
3sa.ArrivalWindowEndTime = slot.Interval.Finish.addSeconds(tz.getOffset(slot.Interval.Finish) / -1000);
4
5update sa;