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

Location based SOQL query using AJAX toolkit

I'm trying to run a location based SOQL query from javascript, using the AJAX toolkit, and I'm getting an error telling me that the location field is not defined. I have geolocation field, called Location__c, defined on the Contact. Here is my code:

<apex:page >
    <script src=""></script>
    <script type="text/javascript" src="../soap/ajax/25.0/connection.js" ></script>
    <script type="text/javascript">
        $(document).ready(function() {
            sforce.connection.sessionId = '{!$Api.Session_ID}';
            var query = "SELECT Id FROM Contact WHERE DISTANCE(Location__c, GEOLOCATION(42.0, -76.0), 'mi') < 10";
            var res = sforce.connection.query(query);

The post request is returning a 500 error, with the message, "No such column 'Location__c' on entity 'Contact'." However, there is a field called Location__c; furthermore, this query works just fine from the Developer Console. Can anyone see what might be going on here?
Ankit AroraAnkit Arora
Let me know if this helps, as he explained the same thing in which you are stuck :
Thanks for the response, Ankit. That didn't actually solve the issue, but I have since figured it out. Since this code was written a while ago, I had to change the API version when loading the AJAX toolkit, since location based queries only work with version 26.0 and up. Changing 

<script type="text/javascript" src="../soap/ajax/25.0/connection.js" ></script>


<script type="text/javascript" src="../soap/ajax/26.0/connection.js" ></script>

did the trick.