Thursday 18 August 2016

GEOLOCATION FOR ACCOUNTS,CONTACTS AND LEADS


DISPLAY ACCOUNTS,CONTACTS,LEADS BASED ON LOCATION
Introduction:
Locate any Salesforce record data Address, anywhere in the world. These searches could be anything from a view of all accounts, clustered by concentrations, to a small neighborhood search for local leads and prospects. Geolocation will locate it (based on either an address or coordinates) and present it to you on an interactive Google Map.

After completing this unit, you’ll be able to:
  • create the latitude and longitude fields for Accounts, Contacts, leads.
  • Find the latitude and longitude of a place.
  • Find the distance based on latitude and longitude.
    CREATE THE LATITUDE AND LONGITUDE  FIELDS FOR ACCOUNTS, CONTACTS, LEADS BY FOLLOWING TABLE.
    S.No
    OBJECT
    FIELD LABEL
        API
      NAME
    DATA
    TYPE
    DECIMAL
    PLACES
    RETURN
    TYPE
    ADVANCED
    FORMULA
    1
    ACCOUNT
    Billing Address Latitude
    Billing_Address_Latitude__c
    Formula
    4
    Number
    BillingLatitude
    2
    ACCOUNT
    Billing Address
    Longitude
    Billing_Address_Longitude__c
    Formula
    4
    Number
    BillingLongitude
    3
    ACCOUNT
    Billing Address
    Accuracy
    Billing_Address_Accuracy__c
    Formula
    0
    Text
    Text( BillingGeocodeAccuracy )
    4
    ACCOUNT
    Shipping Address Latitude
    Shipping_Address_Latitude__c
    Formula
    4
    Number
    ShippingLatitude
    5
    ACCOUNT
    Shipping Address Longitude
    Shipping_Address_Longitude__c
    Formula
    4
    Number
    ShippingLongitude
    6
    ACCOUNT
    Shipping Address Accuracy
    Shipping_Address_Accuracy__c
    Formula
    0
    Text
    Text( ShippingGeocodeAccuracy )
    7
    CONTACT
    Mailing Address Latitude
    Mailing_Address_Latitude__c
    Formula
    4
    Number
    MailingLatitude
    8
    CONTACT
    Mailing Address Longitude
    Mailing_Address_Longitude__c
    Formula
    0
    Number
    MailingLongitude
    9
    CONTACT
    Mailing Address Accuracy
    Mailing_Address_Accuracy__c
    Formula
    0
    Text
    Text( MailingGeocodeAccuracy )
    10
    LEADS
    Address Latitude
    Address_Latitude__c
    Formula
    4
    Number
    Latitude
    11
    LEADS
    Address Longitude
    Address_Longitude__c
    Formula
    4
    Number
    Longitude
    12
    LEADS
    Address Accuracy
    Address_Accuracy__c
    Formula
    0
    Text
    Text( GeocodeAccuracy )

  • SCREEN SHOT FOR ACCOUNT LATITUDE,LONGITUDE,ACCURACY

            
            

    Create same as like this for Contacts and leads.

     Add the fileds to your pagelayouts.

    That's it for the Address field on leads! Now the geocode information associated with Address is visible on lead records. Repeat the same steps to add geocode information to the page layouts for accounts and contacts. Accounts have two address fields, and there are three associated geocode information fields (latitude, longitude, and accuracy rating) for each address.

    GEOCODE FIELDS:

    Geocode information identifies a location using a latitude, a longitude, and an accuracy rating. Geocode fields are available for standard addresses on accounts, contacts, and leads in Salesforce. Geocode fields are not visible on records, but they can be viewed using the Salesforce API.
    Geocodes are added to records using Data.com technology. However, a Data.com license is not required to use this feature
    1.)Setup>> enter Clean Rules in the Quick Find box >> select Clean Rules.

    2.)Edit a geocode clean rule. There are four geocode clean rules available.

    3.)Review your clean rule settings.

    4.)Save the rule.

    5.)Activate the rule.

    6.)If Clean all records when this rule is activated or saved is selected, geocodes are automatically added to all existing records. New records automatically get geocodes when they’re saved. Existing geocode values are overwritten.

    7.)Repeat this process for the other geocode clean rules.

    8.)In Salesforce Classic, if you want to be able to check the clean status of the geocode clean rules, add the Clean This Record with Data.com related list to the page layout for accounts, contacts, and leads. (In Lightning Experience, this step isn’t necessary!)

    9.)Fill the address in account,contacts,lead and press clean in related list of Record Detail Page.
     
    10.) To Integrate the map in to your developer org.


    create visualforce page like this


    <apex:page controller="Mapintegrations" showHeader="true" sidebar="false">
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { width:1000px;height:600px; }
    </style>
    <body>
        <div id="map-canvas"/>
        <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script>
        var map;
       
        function initialize() {
            var mapOptions = {
                center: new google.maps.LatLng(19.337962, 76.684570),
                zoom: 15
            };
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
            plotlocation();
            plotlocationcon();
            plotlocationlead();
        }
       
        function plotlocation() {
            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.Mapintegrations.findAll}',
                function(result, event){
                    if (event.status) {
                        for (var i=0; i<result.length; i++) {
                            var id = result[i].Id;
                            var name = result[i].Name;
                            var lat = result[i].Billing_Address_Latitude__c;
                            var lng = result[i].Billing_Address_Longitude__c;
                            var lati =result[i].Shipping_Address_Latitude__c;
                            var lngi =result[i].Shipping_Address_Latitude__c;
                            addMarker(id, name, lat, lng,lati,lngi);
                        }
                    } else {
                        alert(event.message);
                    }
                },
                {escape: true}
            );
        }   
       
            function plotlocationcon() {
            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.Mapintegrations.findcon}',
                function(result, event){
                    if (event.status) {
                        for (var i=0; i<result.length; i++) {
                            var conid = result[i].Id;
                            var conname = result[i].Name;
                            var conlat = result[i].Address_Latitude__c;
                            var conlng = result[i].Address_Longitude__c;
                            addMarker(conid, conname, conlat, conlng);
                        }
                    } else {
                        alert(event.message);
                    }
                },
                {escape: true}
            );
        }
       
       
            function plotlocationlead() {
            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.Mapintegrations.findlead}',
                function(result, event){
                    if (event.status) {
                        for (var i=0; i<result.length; i++) {
                            var leadid = result[i].Id;
                            var leadname = result[i].Name;
                            var leadlat = result[i].Address_Latitude__c;
                            var leadlng = result[i].Address_Longitude__c;
                            addMarker(leadid, leadname, leadlat, leadlng);
                        }
                    } else {
                        alert(event.message);
                    }
                },
                {escape: true}
            );
        }
       
           function addMarker(id,name,lat,lng,lati,lngi,conid,conname,conlat,conlng,leadlat,leadlng) {
            var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(lat,lng,lati,lngi,conlat, conlng,leadlat, leadlng),
                    map: map,
                    title: name            
            });
            google.maps.event.addListener(marker, 'click', function(event) {
                window.top.location = '/' + id;
            });
        }
       
        google.maps.event.addDomListener(window, 'load', initialize);
       
        </script>
    </body>
    </apex:page>


    CLASS FOR MAP INTEGRATION AND PLOTTING:

    global with sharing class Mapintegrations {
        @RemoteAction
        global static List<Account> findAll() {
            return [SELECT Id,Name,Billing_Address_Latitude__c, Billing_Address_Longitude__c,Billing_Address_Accuracy__c FROM Account];
                }
        @RemoteAction
        global static List<Contact> findcon() {
            return [select Id,Name,Address_Latitude__c,Address_Longitude__c,Address_Accuracy__c FROM Contact];    
        }
        @RemoteAction
        global static List<Lead> findlead() {
           return [select Id,Name,Address_Latitude__c,Address_Longitude__c,Address_Accuracy__c FROM Lead];
     }}




    geo map.png


     


5 comments:

  1. Our Sales persons are always on the road. When they visit a customer (Account), they like to find out is there another customer (Account) nearby where they can visit for maximum utilization of their time. I know this scenario can be automated using the Geolocation feature. I have a formula field on the account object Location_Address__c
    Formula is ShippingStreet + ' ' + ShippingCity + ' '+ ShippingState + ' '+ShippingPostalCode

    On the Account object I have the Shipping Latitude and Shipping Longitude fields on my page layout which gives me the latitude and longitude.

    My requirement is to have a Get NearBy” button on the detail page of Account record or have something similiar which can fethch the nearby accounts.
    When the user will click on this record, we will display a list of nearby Accounts sorted by distance from the current account. The Salesperson can then decide which customers he will like to visit.
    I know we can use SQL query but I am not sure how to populate the nearest accounts.

    I have already activated the Data Integration Rules for account object. I havea activated Geocodes for Account Shipping Address and Geocodes for Account Billing Address for account object.

    ReplyDelete
  2. Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.







    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery






    ReplyDelete
  3. AI-generated captions and real-time human interpretations are important when it comes to consuming content online as they help make event content more accessible to both diverse audiences and attendees. complimentary note and event agreement template

    ReplyDelete