Hello, Is it valid to use bind variables in the Geolocation function?DISTANCE( BillingAddress, GEOLOCATION( :latitude, :longitude ), 'mi' ). I see in the documentation that bind varibles cannot be used for units.
I just found this reference which says bind variables can be used. https://developer.salesforce.com/releases/release/Summer15/queries SOQL and SOSL statements in Apex can reference Apex code variables and expressions if they’re preceded by a colon (:). This use of a local code variable within a SOQL or SOSL statement is called a bind. The Apex parser first evaluates the local variable in code context before executing the SOQL or SOSL statement. Double myLatitude = 10; Double myLongitude = 10; List accountList = [SELECT Id, Name, BillingLatitude, BillingLongitude FROM Account WHERE DISTANCE(My_Location_Field__c, GEOLOCATION(:myLatitude, :myLongitude), 'mi') < 10]; Location myLocation = Location.newInstance(10, 10); Double myDistance = 100; List accountList = [SELECT Id, Name, BillingLatitude, BillingLongitude FROM Account WHERE DISTANCE(My_Location_Field__c, :myLocation, 'mi') < :myDistance];
Binding a variable requires a colon to start the binding. So, the syntax for your function would be:
public void getLocalSites(Decimal lat, Decimal lon, integer dist) {
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
[SELECT Name, Start_Date__c, End_Date__c, ID FROM SiteProgram__c
Where End_Date__c > Today AND
DISTANCE(Location__c, GEOLOCATION(:lat, :long), 'mi') < 20
Order by Name]));
}
All Answers
Apex bind variables aren’t supported for the units parameter in DISTANCE or GEOLOCATION functions. This query doesn’t work. Here is the link for reference : https://help.salesforce.com/apex/HTViewHelpDoc?id=custom_field_geolocate_overview.htm
Please mark this as the best answer if this helps
https://developer.salesforce.com/releases/release/Summer15/queries
SOQL and SOSL statements in Apex can reference Apex code variables and expressions if they’re preceded by a colon (:). This use of a local code variable within a SOQL or SOSL statement is called a bind. The Apex parser first evaluates the local variable in code context before executing the SOQL or SOSL statement. Double myLatitude = 10; Double myLongitude = 10; List accountList = [SELECT Id, Name, BillingLatitude, BillingLongitude FROM Account WHERE DISTANCE(My_Location_Field__c, GEOLOCATION(:myLatitude, :myLongitude), 'mi') < 10]; Location myLocation = Location.newInstance(10, 10); Double myDistance = 100; List accountList = [SELECT Id, Name, BillingLatitude, BillingLongitude FROM Account WHERE DISTANCE(My_Location_Field__c, :myLocation, 'mi') < :myDistance];
Binding a variable requires a colon to start the binding. So, the syntax for your function would be:
Please mark this as the best answer if this helps