Wednesday, October 31, 2018

Territory Management using visual area’s on google maps

How cool would it be, if you can draw area's on google maps and use this for your own territory determination! What if you could simply create your own maps using google mymaps and SAP Sales Cloud would use these area's to determine the corresponding territory.


Prerequisite
When you create a new business partner in your SAP Sales Cloud solution, the system will use the GPS coordinates of the business partner to determine in which area the customer belongs. Having the GPS coordinates of the business partner is a prerequisite for this solution.

The solution

What does the complete solution look like?
  1. Create your own map using google ‘my maps’
  2. Create your own area’s
  3. Download the complete map as a kml file
  4. Upload the kml file into cloud for sales using a custom object
  5. Create a custom field at the business partner to store the area name
  6. Create a custom implementation to detect the area and save the area name into the custom field
  7. Setup the standard territory management using the new custom field

Step 1 - Create your own map
Logon to https://www.google.com/mymaps and create a new map.


Step 2 - Create your own area’s
Draw your own area’s on the map and give them a meaningful name. It is important to make sure the area's do not overlap and there are no gaps between the area's.


Step 3 - Download the complete map as a kml file
From google mymaps you can make a download of all the area's. Using this option a file will be created containing all the area's with there coordinates.

Select from the menu -> Export to KML/KMZ


Make the selections as defined on this print screen and press download


Step 4 - Upload the KML file into SAP Cloud for Sales using a custom object
The KML file contains the exact location of all the lines which makes up the area including the area name.


Define a custom object in SAP Sales Cloud to load all the needed information into the system.


Step 5 - Create a custom field at the business partner to detect the area name
Extend the customer master with a custom text field. This field will be used to fill the area name of the google maps area based on the GPS coordinates of the customer. This will be done using a custom implementation.


Step 6 - Create a custom implementation at business partner
Now it becomes really cool! How can we find the area for the customer GPS location based on the area information from google maps? This mathematic problem is also known as Point in Polygon. Check following explanation How to check if a given point lies inside a polygon
In simple wording the solution is like this:
  • Draw a horizontal line from the GPS location to infinity.
  • Count the number of times an area is crossed.
  • When the number of times it crosses the area is odd it's in the area (or on the edge), otherwise it's outside.
Source: https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/

For the coding we have used the Ray-casting Algorithm. Implement this coding at the customer save event. The implementation should fill the custom field with the area name found.
Step 7 - Setup the standard territory management
Using the standard territories rules including our new custom field, we can identify the territory. As a prerequisite the territory management should be setup including a structure which contains the same territories as define in google maps. At least it should be possible to map the google area's to the territory structure in SAP Sales Cloud. In next example the field 'Area' is our custom field and the field 'Territory' is the standard territory field.


Now you have integrated google maps with territory management in SAP Sales Cloud!