ScheduleService Class
Namespace
Usage
This class calls the scheduling engine and schedules the given service appointment in the highest-scoring available slot.
- SLR or point-to-point predictive travel is the selected routing, and
- Results aren’t stored in the local cache
ScheduleService Methods
ScheduleService includes the following static methods.
schedule(policy, serviceId)
Signature
public static FSL.ScheduleResult schedule(Id policy, Id serviceId)
Parameters
Return Value
Type: FSL.ScheduleResult
Usage
This method schedules the service appointment in the best available slot. If there are no available slots, the appointment isn’t scheduled. This method can be called with only one service appointment at a time. To schedule multiple service appointments, use an Apex batch class. Call this method in batches of one.
If you are using the schedule method with the appointment booking method, perform a time zone conversion. The results of appointment booking are returned in the time zone specified in the method signature. Convert these values back to UTC.
Example
1// FSL.ScheduleService class
2// The Schedule method returns a ScheduleResult result
3FSL.ScheduleResult myResult = new FSL.ScheduleResult();
4
5// static FSL.ScheduleResult Schedule(Scheduling Policy ID, Service Appointment ID)
6myResult = FSL.ScheduleService.schedule(Scheduling Policy ID,Service Appointment ID);
7
8System.debug(myResult);scheduleExtended(policy, serviceId)
Signature
public static List<FSL.ScheduleResult> scheduleExtended(Id policy, Id serviceId)
Parameters
Return Value
Type: List<FSL.ScheduleResult>
Usage
Use this method to schedule two service appointments in a complex work chain. This method respects the complex work setting Use all-or-none scheduling for related appointments. If there are no available slots, appointments in the complex work chain aren’t scheduled.
The scheduleExtended method is valid for a chain of two appointments; if the appointment in serviceId has dependencies with more than one other appointment, scheduling results can be different than expected.
This method can be called with only one service appointment at a time, and runs asynchronously. To examine results that the asynchronous method returns, use the streaming API and subscribe to MstCompletedChannel, the channel for the Field Service managed package.
If Enhanced Scheduling and Optimization (ESO) is enabled:
- The method runs synchronously.
- The method always uses the ESO behavior, which is all-or-none for related appointments scheduling of complex work.
- The method is valid for a chain of up to five to appointments.
If you’re using the scheduleExtended method with the appointment booking method, perform a time zone conversion. The results of appointment booking are returned in the time zone specified in the method signature. Convert these values back to UTC.
getAppointmentInsights
Signature
public static FSL.AppointmentInsightsResult<FSL.AppointmentInsightsResult> getAppointmentInsights(Id policyId, Id serviceAppointmentId))
Parameters
Return Value
Example
The following code sample uses the getAppointmentInsights method to return an AppointmentInsightsResult class that provides details about a specific service appointment that can’t be scheduled on the Gantt.
1
2// FSL.ScheduleService class
3// The getAppointmentInsights method returns a AppointmentInsightsResult result
4FSL.AppointmentInsightsResult myresult = new FSL.AppointmentInsightsResult();
5
6// static FSL.AppointmentInsightsResult
7myresult = FSL.ScheduleService.getAppointmentInsights(Service Appointment ID,Scheduling Policy ID);
8
9System.debug(myresult);scheduleHere(startDate, usedPolicyId, serviceAppointmentId, newResourceId)
Signature
global static List<FSL.ScheduleResult> scheduleHere(Datetime startDate, Id usedPolicyId, Id serviceAppointmentId, Id newResourceId)
Parameters
- startDate
- Type: Datetime
- The desired start date and time to schedule the service appointment. Use the getAppointmentCandidates results to select a time slot. The startDate value must fall within the getAppointmentCandidates scheduling horizon. The default horizon is 10 days and can be configured up to 30 days.
- usedPolicyId
- Type: Id
- The ID of the scheduling policy to use for scheduling rules and optimization.
- serviceAppointmentId
- Type: Id
- The ID of the service appointment to schedule.
- newResourceId
- Type: Id
- The ID of the service resource to assign to the service appointment.
Return Value
Type: List<FSL.ScheduleResult>
Returns a list of ScheduleResult objects containing the scheduled service appointment details, including the scheduled service appointment with updated start and end times, the assigned service resource, the grade or score of the scheduling option, and additional scheduling metadata.
Usage
Use this method after calling the getAppointmentCandidates method to schedule a service appointment to a specific candidate slot and resource.After the service appointment's start time is set, the end time is calculated based on the service duration and the service resource efficiency. To prevent scheduling conflicts, the method performs resource locking and collision detection.
The method handles dependent service appointments in a complex work chain. For service appointments that are part of a complex work chain, all dependent appointments in the chain are scheduled synchronously, and the method returns a ScheduleResult for each. To schedule a chain explicitly, see scheduleExtended. If sliding and reshuffling is enabled, this method applies reshuffle logic during scheduling. A higher-priority incoming appointment can cause an existing appointment to be dropped. See reshuffle.
Example
1
2Id serviceApptId = '08pxx000000001';
3Id resourceId = '0Hnxx000000001';
4Id policyId = '0Uyxx000000001';
5
6Datetime startTime = Datetime.newInstance(2024, 12, 15, 9, 0, 0);
7
8List<FSL.ScheduleResult> results = FSL.ScheduleService.scheduleHere(
9 startTime,
10 policyId,
11 serviceApptId,
12 resourceId
13);
14
15if (results != null && !results.isEmpty()) {
16 FSL.ScheduleResult result = results[0];
17 System.debug('Scheduled Service: ' + result.Service.Id);
18 System.debug('Start: ' + result.Service.SchedStartTime);
19 System.debug('End: ' + result.Service.SchedEndTime);
20 System.debug('Resource: ' + result.Resource.Name);
21 System.debug('Grade: ' + result.Grade);
22}