24/7 Business with Location-Aware data

The Salesforce platform is already managing geolocation fields for a long time. Now it is even easier as we have automatic geocodes for addresses: enable this in setup menu / Data.com Administration / Clean / Clean Rules (if you don't do this, your geolocation data will not be automatically updated, the code below will not work without this data). A typical use case is to identify your accounts that are in a short distance from your location. Another one is to match with Google maps API and identify the path to your customer, and even the duration. All this is fine for local business. But what about the "world company"?
Let say you are in a Call Center, doing lots of outbound calls to your customers, and those are anywhere in the world. The best would be to avoid calling them by night! To avoid this, we need to know what time it is locally for the customer. We have a relationship between a geolocation data and the time zone, then between the time zone and the current time.
We have multiple ways to design the solution. To make the demo easy, we will just use Visualforce without Apex code, but to optimize the solution you should better store the time zone each time the geolocation data is changing, and use this stored time-zone to compute the remote time; this would make Web services calls only to update the time-zone and note each time you want to display the remote time.
Our solution will not require to write any trigger or update the data model. We will just include the Visualforce Page in the standard Page Layout. Just copy/paste the code to create the Visualforce page and use it. The remote time will be updated in real-time without requiring to refresh the page (it is a clock).

Code

<apex:page standardController="Account" showHeader="false" sidebar="false" standardStylesheets="false">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){

//Retrieving the Account location
var Latitude  = {!Account.BillingLatitude};//6.0535185;
var Longitude = {!Account.BillingLongitude};//80.22097729999996;
getTimeUsingLatLng(Latitude,Longitude);
var theOffset=0;

function showIt(){
   var Cur_Date = new Date();
   var UTC = Cur_Date.getTime() + (Cur_Date.getTimezoneOffset() * 60000);
   var Loc_Date = new Date(UTC + theOffset);
   $("#timeOfLocation").html("Local time is " + Loc_Date.toLocaleString());
}

function getTimeUsingLatLng(lat,lng){
  var times_Stamp = (Math.round((new Date().getTime())/1000)).toString();
  $.ajax({
   url:"https://maps.googleapis.com/maps/api/timezone/json?location=" + lat + "," + lng + "&timestamp=" + times_Stamp,
   cache: false,
   type: "POST",
   async: true,
  }).done(function(response){
    if(response.timeZoneId != null){
      theOffset=1000*response.rawOffset + 1000*response.dstOffset;
      window.setInterval(showIt, 1000);
    }
  });
  }
});
</script>
</head>
<body style="margin:0;padding:0;font:bold 12px arial, sans-serif">
  <p id = "timeOfLocation"></p>
</body>
</apex:page>

Explanation

To understand the code, we are using Javascript to call a Google Web Service from the browser, and JQuery to make the code it more readable.
Geolocation data is related to the BillingAddress field. If you change the Billing Address, geolocation will be updated by Salesforce. Our clock is displayed based on these values.
The Apex tag is based on Standard Account Controller: the Visualforce page will be available in the Salesforce Page Layout Editor, you need to drag and drop it into your layout. You have to define a height in your layout of 24 pixels.

Tip

Note: as a quick tip, if for any reason you want to edit yourself the geolocation fields without enabling this new feature, here is a quick Visualforce page snippet:

<apex:page standardController="Account" showHeader="false" sidebar="false" standardStylesheets="false">
<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockButtons >
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
    <apex:inputField value="{!Account.BillingLatitude}"/>
    <apex:inputField value="{!Account.BillingLongitude}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

More...

Further reading on geolocation data in the Salesforce developer Blog

 

© 2018 Jean-Luc Antoine, All Rights Reserved