+ Start a Discussion
CheyneCheyne 

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="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></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";
            console.log(query);
            var res = sforce.connection.query(query);
            console.log(res);
        });
    </script>
</apex:page>

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 : 

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AakZIAS
CheyneCheyne
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>

to 

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

did the trick.