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
Neema Gudur 8Neema Gudur 8 

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.

Best Answer chosen by Neema Gudur 8
Vasani ParthVasani Parth
If it helped you out, please mark the solutions as the best so that community users can land on the right page.

All Answers

Vasani ParthVasani Parth
Neema - Welcome to this vibrant communuity.

Apex bind variables aren’t supported for the units parameter in DISTANCE or GEOLOCATION functions. This query doesn’t work.
String units = 'mi';
List<Account> accountList = 
    [SELECT ID, Name, BillingLatitude, BillingLongitude 
     FROM Account 
     WHERE DISTANCE(My_Location_Field__c, GEOLOCATION(10,10), :units) < 10];
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
 
Neema Gudur 8Neema Gudur 8
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];
Vasani ParthVasani Parth
Aah, I missed it ! Here it is :

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]));
}

Please mark this as the best answer if this helps
Neema Gudur 8Neema Gudur 8
Thank You Parth.
Vasani ParthVasani Parth
If it helped you out, please mark the solutions as the best so that community users can land on the right page.
This was selected as the best answer
Neema Gudur 8Neema Gudur 8
Thank You