Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

i am trying to find nearest stores and their longitude and latitude based on customer location (lon,lat) and populate in Google map. Here is a sample query would work in MySQL. Would this same query work in Salesforce? Do we have Radians function or equivalent function for Radians in salesforce? Moreover, is this possible to frame a query like this?

 SELECT
`id`,
`name`,
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 AS `distance`
FROM `stations`
WHERE
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 < 10
ORDER BY `distance`

Any ideas?

Thanks, Baskaran

share|improve this question
    
This may help: developer.salesforce.com/forums/… –  BarCotter Mar 18 at 11:33

1 Answer 1

up vote 9 down vote accepted

No, there is no radians function available directly in SOQL. However The new release of SOQL includes geolocation cababilities, you do not need to calculate distance yourself. Try something like:

ORDER BY Clause Sort records by distance using a distance condition in the ORDER BY clause. For example:

SELECT Name, StreetAddress_c FROM Warehouse_c WHERE DISTANCE(Location_c, GEOLOCATION(37.775,-122.418), 'mi') < 20 ORDER BY DISTANCE(Location_c, GEOLOCATION(37.775,-122.418), 'mi') LIMIT 10

https://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/index_Left.htm#CSHID=sforce_api_calls_soql_geolocate.htm|StartTopic=Content%2Fsforce_api_calls_soql_geolocate.htm|SkinName=webhelp

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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