Use case for Salesforce Geocodes introduced in Summer'16

In my last post, i shared with you guys about the new geocodes fields introduced by salesforce in Summer'16 release for few standard address fields on Account, Lead, & Contacts.

In this post, i am going to share one use case with you. Had this feature was there back in 2015, when i was working with a client on a mobile app for sales users to help them identify few nearby clients to their existing location, that would have been much easier for me. We had to use google location apis to get co-ordinates of address fields and then select the shortest few clients and display them on the mobile app of the sales team. 

But now, since this new fields are there, this has become a very easy task.

I am gonna share a code snippet with you to display nearest 5 clients to current location of a user.

Here is the agenda of the code:
1) Get users's current location co-ordinates (I am going to use JS for that)
2) Using these co-ordinates to SOQL distance query to get nearest accounts and display them on page


1) get user's current location co-ordinates
I am using JS's navigator library to get geocodes of the user and then passing it to the controller using actionFunction. You can use JS remoting as well to pass the data and get the records

2) Using these co-ordinates to SOQL distance query to get nearest accounts and display them on page

Salesforce distance calculation SOQLs allows you to get records based on the distance from given co-ordinates


1
2
3
4
5
  string query='SELECT Name, billingAddress,billingCity FROM Account WHERE DISTANCE(billingAddress, GEOLOCATION(';
    query+=decimal.valueof(lat)+',';
    query+=decimal.valueof(lon)+'), \'mi\') < 20';
   
        acclist = (list)database.query(query);



Here is the entire code for this;

VF PAGE:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<apex:page controller="MyLocationCtrl" showHeader="false">
   
<html>
<head>
   
    <title>Geocoding Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script>
  function getLocation() {
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(savePosition, positionError, {timeout:10000});
      } else {
          //Geolocation is not supported by this browser
      }
  }

  // handle the error here
  function positionError(error) {
      var errorCode = error.code;
      var message = error.message;

      alert(message);
  }

  function savePosition(position) {
      getAccounts(position.coords.latitude,position.coords.longitude);
  }
  </script>
</head>
<body> <button onclick="getLocation();">Get Nearest 5 Location</button>
    <apex:form >
   
    
    <apex:actionFunction name="getAccounts" rerender="op1" action="{!getaccounts}">
        <apex:param assignTo="{!lat}" value="" name="lat" />
         <apex:param assignTo="{!lon}" value="" name="lon" />
    </apex:actionFunction>
        <apex:outputPanel id="op1">
        <apex:pageBlock>
        <apex:pageBlockTable value="{!acclist}" var="acc">
        <apex:column value="{!acc.id}"/>
            <apex:column value="{!acc.billingCity}" />
       </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
        </apex:form>
</body>
</html>
    
</apex:page>


Contoller:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class MyLocationCtrl {
    public string lat{get;set;} //stores lat
    public string lon{get;set;} //stores lon
    public list<Account> acclist {get;set;} //returns account list
    
    
    public void getAccounts(){
    string query='SELECT Name, billingAddress,billingCity FROM Account WHERE DISTANCE(billingAddress, GEOLOCATION(';
    query+=decimal.valueof(lat)+',';
    query+=decimal.valueof(lon)+'), \'mi\') < 20';
   
        acclist = (list<account>)database.query(query);
       
    }

}
Output:



Using Salesforce geocodes feature in Summer'16 release

In Summer'16 release, salesforce has rolled out much-awaited geocodes feature that allows users to access latitude & longitude co-ordinates on standard addresses fields in few of salesforce objects. Supported objects are : Accounts, Contacts & Leads.



Now, all the existing and new records will have this lat and lon fields autopopulated based on the address provided there. These fields can not be added to layout directly. you would need to create formula to access their values on the page.

Steps to create formula : 
1) Create a new formula field of type Number with 4 decimal places
2) In the formula editor, select fields and select BillingAddressLatitude field to view Billing Address Latitude co-ordinates. 


Likewise, other formula fields can be created to capture longitude and geocodeaccuracy fields.

How do you intend to use these new fields? Share your thoughts



Making external images work in salesforce visualforce pages renderas="pdf"

When you use some external images url in a visualforce page that is being rendered as pdf, you will notice that external images are not getting displayed on the page, instead a "No-image" icon gets displayed.






Solution:
Salesforce PDF generation engine that runs in the background, runs in same security settings as Apex, which means you have to whitelist the image hosting site by going to setup->Security Controls ->Remote site settings


Refresh the page, and here you go!!


May the Sales-FORCE be with you!!