Newer Version Available

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

Code Examples: Dispatcher Console Custom Actions

Learn how to configure Apex classes or Visualforce pages that you want to link to a custom action in the dispatcher console.
Custom actions can either call an Apex class or open a Visualforce page, and can be run on records in several areas of the dispatcher console. To learn how to create custom actions, see Create Custom Actions for the Dispatcher Console.

All Apex classes implementing the quick action interfaces must be declared as Global to be accessible from the dispatcher console.

Note

Creating Apex Classes

When you create an Apex class to link to a dispatcher console custom action, implement one of the following three interfaces in your class.

Interface Description
CustomGanttServiceResourceAction For actions on service resources. The parameters are the service resource record ID, the service territory member record ID reflected on the Gantt, and the start and end dates of the current Gantt view. No additional parameters are included.

Use the following format: String action(Id resourceId, Id stmId, Datetime strGanttStartDate, Datetime strGanttEndDate, Map<String, Object> additionalParameters)

CustomGanttServiceAppointmentAction For actions on service appointments. The parameters are the assigned resource record IDs—used for bulk actions— and the start and end dates of the current Gantt view. No additional parameters are included.

Use the following format: String action(List<Id> serviceAppointmentsIds, Datetime strGanttStartDate, Datetime strGanttEndDate, Map<String, Object> additionalParameters)

When this action is implemented, multiple service appointments can be returned. In your method, we recommend creating an if statement to check how many IDs are returned. First, validate that at least one ID was returned: serviceAppointmentsIds.size()>1. Then, you can take different actions depending on whether 0, 1, or more appointment IDs were returned.

CustomGanttResourceAbsenceAction For actions on resource absences. The parameters are the resource absence record ID, the absence type (‘na’ or ‘break’), and the start and end dates of the current Gantt view. No additional parameters are included.

Use the following format: String action(Id absenceId, String absenceType, Datetime strGanttStartDate, Datetime strGanttEndDate, Map<String, Object> additionalParameters)

These functions must be global and require that a string be returned. If the string isn’t empty, it is used in the Gantt notification shown when a user clicks the related action.

Code Example: Service Resource Custom Action

This action creates a resource absence of type Non Availability for the selected service resource that spans the days visible on the Gantt.

1global class BlockResourceVisibleTime implements FSL.CustomGanttServiceResourceAction {
2
3    global String action(Id resourceId, Id stmId, Datetime ganttStartDate, Datetime ganttEndDate, Map<String, Object> additionalParameters) {
4
5        ResourceAbsence na = new ResourceAbsence();
6
7        // get Resource Absence record type - NA
8        RecordType recordTypeNA = [
9            SELECT 
10                Id, SobjectType, Name 
11            FROM 
12                RecordType 
13            WHERE 
14                DeveloperName =: 'Non_Availability' 
15                AND 
16                SObjectType =: ResourceAbsence.getSobjectType().getDescribe().getName() 
17            ];
18
19        na.RecordTypeId = recordTypeNA.Id;
20        na.ResourceId = resourceId;
21        na.FSL__Approved__c = true;
22        na.Start = ganttStartDate;
23        na.End = ganttEndDate;
24
25        insert na;
26
27        ServiceResource resource = [SELECT Name FROM ServiceResource WHERE Id =: resourceId];
28
29        return 'Blocked availability to ' + resource.Name + ' from ' + ganttStartDate.format() + ' to ' + ganttEndDate.format();
30
31    }
32
33}

Code Example: Service Appointment Custom Action

This action toggles the In Jeopardy field between True and False.

1global class toggleServiceAppointmentJeopardy implements FSL.CustomGanttServiceAppointmentAction {
2 
3    global String action(List<Id> serviceAppointmentsIds, Datetime ganttStartDate, Datetime ganttEndDate, Map<String, Object> additionalParameters) {
4       
5        List<ServiceAppointment> saList = [SELECT FSL__InJeopardy__c, AppointmentNumber FROM ServiceAppointment WHERE Id in : serviceAppointmentsIds];
6        String reply = '';
7        List<String> saNames = new List<String>();
8
9        for (ServiceAppointment s : saList) {
10            s.FSL__InJeopardy__c = !s.FSL__InJeopardy__c;
11            saNames.add(s.AppointmentNumber);
12        }
13       
14        upsert saList;
15
16        reply = String.join(saNames, ', ');
17        return 'Service Appointments successfully processed: ' + reply;
18    }
19   
20}

Code Example: Resource Absence Custom Action

For resource absences of type NA, this action creates a duplicate absence on the following day.

1global class copyAbsenceToNextDay implements FSL.CustomGanttResourceAbsenceAction {
2 
3    global String action(Id absenceId, String absenceType, Datetime ganttStartDate, Datetime ganttEndDate, Map<String, Object> additionalParameters) {
4       
5        ResourceAbsence resourceAbsence = [SELECT Id, AbsenceNumber, Start, End, ResourceId, RecordTypeId, FSL__Approved__c FROM ResourceAbsence WHERE Id =: absenceId LIMIT 1];
6
7        ResourceAbsence raClone = resourceAbsence.clone(false, true, false, false);
8        raClone.Start = resourceAbsence.Start.addDays(1);
9        raClone.End = resourceAbsence.End.addDays(1);
10        raClone.ResourceId = resourceAbsence.ResourceId;
11        raClone.RecordTypeId = resourceAbsence.RecordTypeId;
12        raClone.FSL__Approved__c = true;
13        insert raClone;
14
15        return 'Resource Absence successfully copied.';
16    }
17   
18}

Creating Visualforce Pages

When you create a Visualforce page, use the following GET parameters.

For actions on... Description
Service appointments Use the following format: services [if multiple], id (comma delimited if multiple), start (current Gantt start date, string), end (current Gantt end date, string)

Example for a Visualforce page used to update a single service appointment: ?id=08p4E000000Kj5hQAC&​start=5-7-2018&end=5-8-2018

Example for a Visualforce page used to update multiple service appointments: ?services=08p4E000000Kj5hQAC,08p4E000430Kj5hAPP&​start=5-7-2018&end=5-8-2018

Service resources Use the following format: id, stm (ID of service resource’s current service territory member record), start (current Gantt start date, string), end (current Gantt end date, string)

Example: ?id=0Hn4E0000001OMQSA2&​stm=0Hu4E0000005cpPSAQ&start=5-7-2018&​end=5-8-2018

Resource absences Use the following format: id, type (’break’ or ‘na’), start (current Gantt start date, string), end (current Gantt end date, string)

Example: ?id=0Hw4E00000091HSSAY&​type=break&start=5-7-2018&​end=5-8-2018

To close the Visualforce lightbox from your code, use: parent.postMessage('closeLightbox','*');

Custom dispatcher console actions can’t open Visualforce pages that are part of a managed package.

Note