Skip to main content

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.

Display Custom Actions Where Users Need Them

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: toggle Service Jeopardy
    • Name: toggle_Service_Jeopardy
    • Description: Grant permission to view and use the toggleServiceJeopardy class on the Gantt.
  6. Click Save.

Then she creates a custom permission set for her lead dispatchers and adds her newly created permission.

  1. Click Setup and select Setup.
  2. In the Quick Find box, enter permission and click Permission Sets.
  3. Click New.
  4. For Label, enter Lead Dispatcher and click Save.
  5. In that tab, scroll down and click Custom Permissions.
  6. Click Edit.
  7. Select toggle Service Jeopardy and click Add.
    Lead Dispatchers permission set showing the toggle Service Jeopardy permission added to the list of enabled permissions.
  8. Click Save.
  9. Click Manage Assignments.
  10. Click Add Assignments.
  11. Check the box next to your name and click Assign.

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: toggle Service Jeopardy
    • Icon: Select an icon to display next to the action label.
      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.

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.

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities