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
Here is the entire code for this;
VF PAGE:
Contoller:
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:
No comments:
Post a Comment