Erfassen Sie Ihre Fortschritte
Trailhead-Startseite
Trailhead-Startseite

Add a Custom Action

Learning Objectives

After completing this unit, you’ll be able to:

  • Define the functionality and location of a custom action.
  • Create an Apex class using sample code.
  • Set permissions for the Apex class.
  • Add a custom action to the dispatcher console.

What You Need, Where You Need It

Jacinta comes to Maria with another request. Sometimes, mobile workers contact her to tell her that they’re ill or an appointment is running over. These schedule changes jeopardize their next appointments, putting them at risk of not being completed today. Jacinta wants to set those appointments’ status to In Jeopardy right from the Gantt. Can Maria help?

You bet she can. Maria can create a custom action and add it to Jacinta’s Gantt to set appointment status. Custom actions can either call an Apex class or open a Visualforce page. They can be run on records in several areas of the dispatcher console. Maria decides that this action is best accomplished using an Apex class.

Maria asks Jacinta where she’d like to see the custom action. Maria lists the places where an admin can add custom actions to the dispatcher console.

  • The service appointment list on individual or multiple appointments
  • The Gantt on individual or multiple appointments
  • The Gantt on individual mobile workers or resource absences
  • Polygons on the map (the action runs on the appointments within a polygon)

Jacinta requests that Maria add the custom action to individual service appointments on the Gantt. Maria is ready for the next steps.

  1. Write the Apex class.
  2. Assign permissions to the class.
  3. Add the custom action to the dispatcher console.

Write an Apex Class

Maria checks to see if the code for Jacinta’s request already exists. She’s thrilled to find an Apex class code sample that does just what she needs in the Field Service Developer Guide. She uses the toggleServiceAppointmentJeopardy class provided there to get the job done.

  1. Click Setup and select Developer Console.
  2. From the File menu, select New | Apex Class.
  3. For the class name, enter toggleServiceAppointmentJeopardy and then click OK.
  4. Select all the code in the developer console and delete it.
  5. Enter the new code. Maria pastes in the code sample.
    global class toggleServiceAppointmentJeopardy implements FSL.CustomGanttServiceAppointmentAction {
    global String action(List<Id> serviceAppointmentsIds, Datetime
    	ganttStartDate, Datetime ganttEndDate, Map<String, Object>
    	additionalParameters) {   
    List<ServiceAppointment> saList = [SELECT 
    	FSL__InJeopardy__c, AppointmentNumber FROM
    	ServiceAppointment WHERE Id in : serviceAppointmentsIds];
    	String reply = '';
    	List<String> saNames = new List<String>();
    for (ServiceAppointment s : saList) {
    		s.FSL__InJeopardy__c = !s.FSL__InJeopardy__c;
    		saNames.add(s.AppointmentNumber);
    	}
    	
    upsert saList;
    reply = String.join(saNames, ', ');
    	return 'Service Appointments successfully processed: ' + 
    	Reply;
    }
    }
    
  6. Select File | Save.

Maria’s new class is saved in the list of Apex classes.

Assign Permissions

Although Maria has created the class, she can’t add it to the dispatcher console until she sets up the proper permissions.

First, she sets up custom permissions for her new Gantt custom action.

  1. Click Setup and select Setup.
  2. In the Quick Find box, enter permission.
  3. Select Custom Permissions.
  4. Click New.
  5. Enter the label, name, and description. Maria enters the following.
    • Label: Gantt toggle Service Jeopardy
    • Name: Gantt_toggle_Service_Jeopardy
    • Description: Grant permission to view and use the toggleServiceJeopardy class on the Gantt.
  6. Click Save.

Then she assigns the new permission to the correct permission set for her dispatchers.

  1. Click Setup and select Setup.
  2. In the Quick Find box, enter permission.
  3. Select Permission Sets.
  4. Select the permission set to grant access to. Maria has already created a permission set for lead dispatchers and assigned it to Jacinta and her other lead dispatchers. To use that permission set, she selects Lead Dispatcher Permissions.
  5. Click Custom Permissions.
  6. Click Edit.
  7. Select Gantt toggle Service Jeopardy and click Add.

    Lead Dispatchers permission set showing the Ganttt toggle Service Jeopary permission added to the list of enabled permissions.

Maria has the correct permissions to add the action to the dispatcher console. One more step and she’s done.

Add the Action to the Dispatcher Console

Maria is ready to add that new action to the dispatcher console so Jacinta can see it.

  1. From the App Launcher (App Launcher icon), open Field Service Admin.
  2. Click the Field Service Settings tab.
  3. Open Dispatcher Console UI and click Custom Actions.
  4. In the left-hand column, select an action category to define the location and scope of the action. Maria selects Gantt.
  5. Click New Action and enter the details. Maria enters the following.
    • Label in Dispatcher Console: Change Jeopardy Status
    • Action Type: Apex Class
    • Apex Class: toggleServiceAppointmentJeopardy
    • Required Custom Permission: Permission to use ToggleServiceJeopardy
    • Icon: Select an icon to display next to the action label.

    Field Service settings displaying the Custom Actions tab with the values filled out.

  6. Click Save.

Maria tells Jacinta that her new action is ready to go. When Jacinta right-clicks an existing service appointment on the Gantt, she sees an option to change its jeopardy status. To select multiple service appointments, she holds down the Ctrl key and selects them.

Note

Note

If you’re following along in your Trailhead playground, you don’t have any service appointments. Create one and assign it to James Clifton to see the new menu item.

Right-click menu for a service appointment in the Gantt opened to show the Change Jeopardy Status option added to the bottom of the menu

Maria added an action to the Gantt. She defined the action, decided where to put it, found a code sample that did what she needed, and set the permissions. Then she added it to the Dispatcher Console UI setting. Now Jacinta can change service appointment jeopardy statuses as needed.

Maria and Jacinta have customized the dispatcher console to meet the team’s needs. Remember when we asked you to think about ways to customize your own dispatcher console? We bet you now have some great ideas.