Set Territories Using Polygons
Set Territories Using Polygons
New, prod-usable: Many Salesforce Field Service customers choose to leverage the feature "Use polygons to assign service
Set Emergency and territories" under Field Service Settings to set the Service Territory on Service Appointments based on the map
polygon (and associated territory) that the appointment's geolocation falls within. This feature is particularly
Multi-Day Service useful when an organization has many geographies and territories that they provide coverage in, and it is not
Appointment Fields feasible for an end user to set the appropriate service territory.
Label and Gantt Icon Territory that covers a particular geography, however the feature only supports returning a single territory (the
lowest or highest in a hierarchy) and a single map polygon even if multiple map polygons and territories cover
Automatically the same geographical area.
New, prod-usable:
Set Parent Record
Purpose of this Automation/Trigger
Object Type (Work Some organizations have two or more territories that overlap the same geography (for example a utility customer
Order or WOLI) which may have "Boston, MA - Gas" and "Boston, MA - Electric" territories) which can present challenges in
leveraging this feature alone to set the Service Territory. For example, while a utility customer may be able to
Updated, prod- differentiate the types of service they perform (new install, maintenance, repair) via attributes on the Service
usable: Set Appointment for scheduling and reporting purposes, given Gas and Electric lines of business may be completely
Territories Using separate, resources not shared, different dispatching and optimization processes, viewing experience needs from
Geography
the Dispatch Console, and more - it makes sense to use different Service Territories for each line of business.
(Polygons) and In this case, the customer would have two territories covering the same geography. Given only a single territory
Additional Logic can be associated to a map polygon record however, we would need additional logic to look at another attribute
on the Service Appointment - in addition to geography - in order to correctly associate the Service Territory
Updated, prod- considering line of business.
usable: Translate
Appointment Times Solution
to Local Times This tool solves the above challenge and customer use case by doing the following:
Updated, prod-
usable: Email a 1. A single map polygon and geographical territory record should exist for every geography that the customer
Service Report
provides coverage in.
Prod-usable: 2. A territory(ies) that represent the concatenation of region + line of business/type of work should exist.
Respond to 3. The Service Appointment should have a geolocation that correlates to/falls within a geographical
Absences: Adjust territory/polygon.
Service Crew
Membership 4. The Service Appointment should have a way of differentiating which line of business it falls within (E.g. Gas
or Electric for a utility customer).
Prod-usable:
Programmatically 5. The setting "Use polygons to assign service territories" should be enabled in the org.
Generate Service
Reports
6. The below trigger should be created in the org, and adapted to reference whatever additional logic or attributes
define business line, naming conventions that you'd like to use, etc. In the below example, we use a
concatentation of the original geographical territory name which the polygon method returns (E.g. "Atlanta")
with the name of the Work Type on the Service Appointment (E.g. "Lawn Maintenance") to find the
corresponding Service Territory that exists in the system but couldn't be retrieved by polygonUtils() alone (E.g.
"Atlanta - Lawn Maintenance") and set it on the Service Appointment.
Enjoy!
https://salesforceenthusiast.com/Pages/code.php 1/4
1/10/24, 3:09 PM Salesforce Enthusiast - Field Service Lightning, Solution Architecture and Development on the Salesforce Platform
Salesforce Enthusiast
2
3
Field Service
trigger serviceAppointmentUpdates
before insert) {
on ServiceAppointment (before update,
Package Step 2: Create the below Apex class, or add code lines within asterisks to existing class
New, prod-usable:
Set Emergency and
Multi-Day Service
Appointment Fields
New, prod-usable:
Set Service
Appointment Gantt
Label and Gantt Icon
Automatically
New, prod-usable:
Set Parent Record
Object Type (Work
Order or WOLI)
Updated, prod-
usable: Set
Territories Using
Geography
(Polygons) and
Additional Logic
Updated, prod-
usable: Translate
Appointment Times
to Local Times
Updated, prod-
usable: Email a
Service Report
Prod-usable:
Respond to
Absences: Adjust
Service Crew
Membership
Prod-usable:
Programmatically
Generate Service
Reports
https://salesforceenthusiast.com/Pages/code.php 2/4
1/10/24, 3:09 PM Salesforce Enthusiast - Field Service Lightning, Solution Architecture and Development on the Salesforce Platform
Salesforce Enthusiast
2
3
public class Field Service{
updateRecords
Package 13
14
//create map of territory names as key and ids as values
map<string, id> territoryNameAsKeyMap = new map<string, id>();
New, prod-usable: 15
Set Emergency and 16 //create map of territories
Multi-Day Service 17 map<id, serviceterritory> territoryMap = new map<id,
Appointment Fields 18
19
serviceterritory>();
Automatically 24
25 }
territoryMap.put(st.id, st);
Order or WOLI) 29
30
execution)
for(serviceappointment sa: newServiceAppointments){
Updated, prod- 31
usable: Set 32 //**************************************************
Territories Using 33 //Set Territories Using Polygons and Additional Logic
Geography 34 (Geography & Business Unit)
(Polygons) and 35
Additional Logic 36
37
// USE CASE: You may have several lines of business that are
distinct territories, serving the same geography. The PolygonUtils()
Updated, prod- 38 method/feature allows you
usable: Translate 39 // to set the Service Territory on the Service Appointment
Appointment Times 40 based on the Service Territory associated to a map polygon that the Service
to Local Times 41
42
Appointment's lat/lon coordinates
// fall within. However, if you have multiple territories for
Updated, prod- 43 different lines of business (E.g. Atlanta - Tree Service, Atlanta - Lawn
usable: Email a 44 Maintenance) that overlap the same
Service Report 45 // geography via polygon records, you need additional logic in
Prod-usable:
46 order to tell the system which territory to use (E.g. Tree Service or Lawn
47 Maintenance).
Respond to 48 //
Absences: Adjust 49 // PURPOSE: This code allows you to set the Service Territory
Service Crew 50 on a Service Appointment to a custom/specific value AFTER
Membership 51
52
// polygon classification occurs, but also BEFORE scheduling
options/candidates are returned. This allows you to:
Prod-usable: //
Programmatically // a.) receive the geography polygon/territory that the job
Generate Service geolocation falls within (E.g. Atlanta)
Reports // b.) set the new territory (E.g. Atlanta - Tree Service)
based on additional attributes on the Service Appointment (in this case,
Work Type name was used)
// c.) receive candidates after the scheduling engine has
considered the NEW territory on the Service Appointment (the old one is not
used, which avoids any downstream issues e.g. violations on the Gantt,
etc.)
//
// Note that this code can be adapted to reference other
attributes on the SA or the Work Type with small changes.
https://salesforceenthusiast.com/Pages/code.php 3/4
1/10/24, 3:09 PM Salesforce Enthusiast - Field Service Lightning, Solution Architecture and Development on the Salesforce Platform
AppExchange }
Package }
//**************************************************
New, prod-usable: }
Set Emergency and }
Multi-Day Service
Appointment Fields
New, prod-usable:
Set Service
Appointment Gantt
Label and Gantt Icon
Automatically
New, prod-usable:
Set Parent Record
Object Type (Work
Order or WOLI)
Updated, prod-
usable: Set
Territories Using
Geography
(Polygons) and
Additional Logic
Updated, prod-
usable: Translate
Appointment Times
to Local Times
Updated, prod-
usable: Email a
Service Report
Prod-usable:
Respond to
Absences: Adjust
Service Crew
Membership
Prod-usable:
Programmatically
Generate Service
Reports
https://salesforceenthusiast.com/Pages/code.php 4/4