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
Jack OdellJack Odell 

Passing variables to GEOLOCATION when doing a DISTANCE query in Apex

Hey folks.

I'm having trouble getting GEOLOCATION to accept variables and I'm wondering if I'm doing something wrong or whether I've run into some sort of limitation.

When I pass the lat/longs as literals, the code works without a problem:
Store__c[] sList = [SELECT Queue__c FROM Store__c WHERE DISTANCE(Location__c, GEOLOCATION(42.30,-71.28), 'mi') < 100 LIMIT 1];   

However, if I need to be able to pass the latitude and longitude as variables.  When I do I get errors.  I would have expected prepending my variables with colons would have worked (see below), but I get this error:"unexpected token: ":".
Store__c[] sList = [SELECT Queue__c FROM Store__c WHERE DISTANCE(Location__c, GEOLOCATION(:dLat,:dLon), 'mi') < 100 LIMIT 1];

Am I doing something wrong?  Or does the GEOLOCATION function not accept variables being passed to it? 

In the worst case, I could load my lat/longs into a custom object, but I had been hoping to avoid that and use a custom setting instead.
Best Answer chosen by Jack Odell
Jack OdellJack Odell
I was able to get around the issue using the Database.query method (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.htm) and building the query string manually.