Monthly Archives: August 2016

Plot your customers using Geocodes for Addresses

Introduction
With summer’ 16 release salesforce introduced a new feature Geocodes for Addresses which enables users to get geocodes added for addresses on existing and new accounts, contacts, and leads.

How is it useful for my organization ?
A. This can help your organization’s salesforce users/ sales reps to identify prospects in a particular area.
B. This can help to set up campaigns based on location.
C. This can your business to assign leads or opportunity to your sales reps.
D. You can identify distance between you and your customer locations.

How it can be used?
Currently this feature is rolled out to following standard address fields ->
A.Billing and Shipping Address Fields on Accounts object.
B.Address Field on Leads object.
C.Mailing Address Field on Contacts Field.

How does it works?
Its works with no extra efforts. You just need to supply the address information in supported address fields and geocodes are automatically generated for the particular record.

The clause is you should have Geocode Clean Rules activated. Once activated salesforce generates the address coordinates on the existing records and new records in supported objects

What can be an example for this?
Let say you enter Amsterdam, Netherlands as Billing Address on an account record.
Now salesforce will generate following values in the geocode fields
BillingLadtitude -> 52.37317
BillingLongitude -> 4.89066

Note that these fields act as part of BillingAddress field a.k.a Address Compound Fields

How I can verify the values are correct?
You can use http://www.latlong.net/Show-Latitude-Longitude.html

What can be a business use case?
Lets say you want to plot your customers (Accounts) on a graph based upon the distance between your organization’s HQ and customer location based on Billing Address.

How it can be done?
We can do it using visualforce and apex. I will explain this using my previous post Simple dashboard using Bootstrap + Chart.js inside Salesforce.com

Using the same I have plotted Number of customers v/s distance graph

I have created buckets of distance in form of –
A. Less than 50 Miles.
B. Between 50 and 100 Miles.
C. Between 100 and 150 Miles.
D. More than 150 Miles.

The location from which I am calculating the distance is Gurgaon,Haryana,India which equals to location coordinates as Longitutde -> 28.4247649 Latitude -> 76.8496956

How it is possible in code?

For calculating the distance we are using the Location Based SOQL Query and Geocode for Address Fields

The query looks like ->
SELECT id,name FROM Account where DISTANCE(BillingAddress, GEOLOCATION(28.4247649, 76.8496956), 'mi')<50

The DISTANCE is being used to calculate the difference between the BillingAddress Coordinates and Gurgaon's coordinates, mi denotes the unit of measurement is Miles, it can be changed with Kilometers(km).

Where is the code?

Here you go ->

Visualforce Page ->


    
    
        
            Charts
            
            
            
            
        
      
          
         

Apex Class ->

public class GeoChartCreatorCls {
    public List accList;
    public Integer lessThan50 {get;set;}
    public Integer range50To100 {get;set;}
    public Integer range100To150 {get;set;}  
    public Integer moreThan150 {get;set;}
    public geoChartCreatorCls(){
        accList = new List();
        lessThan50 = 0;
        range50To100 = 0;
        range100To150 = 0;
        
              
        generateData();
        
    
    }
    
    
   
   public void generateData(){
   
       accList  = [SELECT id,name FROM Account where DISTANCE(BillingAddress, GEOLOCATION(28.4247649, 76.8496956), 'mi')<50];
       
       if(accList!=null && accList.size()>0){
           lessThan50 = accList.size();
       }
       
       accList  = [SELECT id,name FROM Account where DISTANCE(BillingAddress, GEOLOCATION(28.4247649, 76.8496956), 'mi')>50 AND DISTANCE(BillingAddress, GEOLOCATION(28.4247649, 76.8496956), 'mi')<100];
       
       if(accList!=null && accList.size()>0){
           range50To100 = accList.size();
       }
          
       
       accList  = [SELECT id,name FROM Account where DISTANCE(BillingAddress, GEOLOCATION(28.4247649, 76.8496956), 'mi')>100 AND DISTANCE(BillingAddress, GEOLOCATION(28.4247649, 76.8496956), 'mi')<150];
       
       if(accList!=null && accList.size()>0){
           range50To100 = accList.size();
       }
           
       
       accList  = [SELECT id,name FROM Account where DISTANCE(BillingAddress, GEOLOCATION(28.4247649, 76.8496956), 'mi')>150 ];
       
       if(accList!=null && accList.size()>0){
           range50To100 = accList.size();
       }
       
       
   }

}

Do geocode fields works with Person Accounts?
No.

Any issue encountered while working with this?
The only issue I found while working with DISTANCE is, it wont supports = [equals to],>= [greater than equals to] and <= [less than equals to]