Finding Salesforce Objects Near Me

Home > Finding Salesforce Objects Near Me

Finding Salesforce Objects Near Me

Posted on
  • Font size: Larger Smaller
  • Hits: 3431
  • Print

In the lab, we've been able to give users the ability to see Salesforce objects around the user. For example, if a Sales person selects "Show Contacts Near Me" in their Salesforce1 application, a map will pop up and display their location (via GPS), and then map all of the Contacts within a specified mile radius. The idea is that a busy Sales person can use this functionality to see other Contacts in close proximity, mapped on their phone. Presumably they could make a visit, resulting in higher productivity, and possibly additional revenue.

We realize that the Salesperson may want to see other Objects than just the Contacts. Through the code, we can define access to other Salesforce objects such as Accounts and Leads, as well as custom objects that have a geo-coding attribute.

Furthermore, we expect the Salesperson may want to see their Objects in proximity to their current position, filtered by values or more complex business rules. For example, the Salesperson may want to see their Accounts sorted by highest revenue first, or last touched first. They may want to employ more complex rules that take into account many variables that would rank a customer visit higher than another.

At the core of the mapping page is the relatively simple SOQL query (wrapped in an Apex static method):

    @RemoteAction
    global static Contact[] FindContacts(Decimal lat, Decimal lng, Integer miles) {
        string query = 'SELECT Id, Name, Phone, MailingStreet, MailingCity,  Location__Latitude__s, Location__Longitude__s  FROM Contact '+
                    'WHERE DISTANCE(location__c, GEOLOCATION('+lat+','+lng+'), \'mi\') < '+miles+' '+
                    'ORDER BY DISTANCE(location__c,  GEOLOCATION('+lat+','+lng+'), \'mi\')';
        return Database.query(query);
    }

A Geolocation field called “location” was added to the Contact object to facilitate this. To invoke this method from the page, the Javascript API was used.  By virtue of making these global, static methods with the @RemoteAction annotation, they are available to in javascript directly:

EntitiesNearMeController.FindContacts(position.coords.latitude, position.coords.longitude, sv, function (result, event) {
        if (event.status) {
            //Perform work on successful event
        }
    });

In that, the result variable is the list of Contacts that are within the range specified.  The Lat/Lng is retrieved with the standard javascript call:

navigator.geolocation.getCurrentPosition(function(){});//Put your position-dependent function inside

Then it’s just a matter of wiring up the google maps code to draw markers and infowindows and, if desired, populating a list of results as well.

Here's a screenshot of this code in action. The green arrow shows my current position, the yellow pins show my Contacts near me, and the green pins show my Accounts near me. This is rendered in Salesforce1 on the iPhone, and an approximation of what a Salesperson might experience and be able to use when they call this up.

 

geo-locate 

 ##########