function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramesh KallooriRamesh Kalloori 

Dynamic SOQL Distance query?

Hi All,

i want pass longitude and latitude values dynamically for the below query.

List<Account> Acc=[select Id, Name, BankNameSpace__Location__Latitude__s, BankNameSpace__Location__Longitude__s from Account where DISTANCE(BankNameSpace__Location__c, GEOLOCATION(17.437, 78.453 ), 'mi') < 10  and BankNameSpace__Latitude__c!=null limit 2];

if i modify the query below iam getting error.

Decimal lat=17.437;
Decimal lng=78.453;
List<Account> Acc=[select Id, Name, BankNameSpace__Location__Latitude__s, BankNameSpace__Location__Longitude__s from Account where DISTANCE(BankNameSpace__Location__c, GEOLOCATION(lat,lng ), 'mi') < 10  and BankNameSpace__Latitude__c!=null limit 2];

thanks,
RAmesh

Alex TennantAlex Tennant
You cannot bind variables that are part of the GEOLOCATION and DISTANCE formulas using the standard colon syntax, however you can use a dynamic SOQL query to do this:

Decimal lat = 17.437;
Decimal lng = 78.453;
List<Account> Acc = Database.query('SELECT Id, Name, BankNameSpace__Location__Latitude__s, BankNameSpace__Location__Longitude__s FROM Account WHERE DISTANCE(BankNameSpace__Location__c, GEOLOCATION(' + lat + ', ' + lng + ' ), \'mi\') < 10  and BankNameSpace__Location__Latitude__s != null limit 2');