0

My current location is stored in currentPosition variable in terms of latitude and longitude.

public String currentPosition{get;set;}////37.77493,-122.419416

I have enabled Geolocation codes for Account billing Address feature to get the Account address in terms of latitude and longitude(BillingLatitude,BillingLongitude)

How to Query the account records near to me in 50kms radius using BillingLatitude&BillingLongitude? Can anyone help me?

3 Answers 3

0

You will need this:

SELECT
    Account_Address
FROM
    Your_Account_Table
WHERE
    ROUND(ROUND(geography::Point(CurrentPositionLatitude, CurrentPositionLongitude, 4326).STDistance(geography::Point(BillingLatitude, BillingLongitude, 4326)), 2)/1609.344, 2) < 50
2
  • It measures the distance in miles though Aug 11, 2017 at 8:03
  • From review: Adding a bit more details and an explanation on how this solves the problem might help. Thank you! Aug 11, 2017 at 8:05
0

You can use the below link, which describes solution to your requirement in a very precise way, and it can help with other things you might need as you are playing with Location.

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_geolocate.htm

0

You'll want to use the GEOLOCATION and DISTANCE filters, outlined here:

String[] parts = currentPosition.split(',');
Decimal latitude = Decimal.valueOf(parts[0]), longitude = Decimal.valueOf(parts[1]);
Account[] results = [
    SELECT Name FROM Account 
    WHERE DISTANCE(BillingAddress, GEOLOCATION(:latitude, :longitude), 'km') < 50];

I don't have any good data to test with, but this should get you started.

4
  • unexpected syntax: 'missing RPAREN at ',''
    – Mahesh M
    Aug 11, 2017 at 8:49
  • expecting a right parentheses, found ','
    – Mahesh M
    Aug 11, 2017 at 8:49
  • Hi, I am getting the above two errors when i am trying to execute the above query
    – Mahesh M
    Aug 11, 2017 at 8:50
  • @MaheshM Yes, there was a missing ")". I added it into the code above.
    – sfdcfox
    Aug 11, 2017 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.