Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm using the new Geolocation field data type and SOQL DISTANCE function in a Visualforce page. I'm using the SOQL with DISTANCE to retrieve records into a standardSetController. It works great - records are retrieved and displayed successfully.

However, when I run unit tests, I get inconsistent failures. When I run in the IDE, the unit test runs fine. Then I go immediately to the UI and run the same unit test and I get a failure -- No such column Location__c on entity Person__c -- on the SOQL statement with DISTANCE. (Location__c is the Geolocation field, of course.)

Then I immediately go to the IDE to run the unit test, and now it fails with the same error.

So I comment out sections in the unit test, deploy the VF code to production, and the page works great. But of course I have very low test coverage on the controller.

Why does the VF page work fine in the UI, but the unit test fail? Does it have to do with the way test data is set up in the unit test? But even if the Location_Latitude_s and Location_Longitude_s fields are null, the SOQL query shouldn't throw a "no such column" error.

I'm using API 26 in the IDE. I'm on sandbox cs7.

My basic SOQL statement is SELECT DISTANCE(Location__c, GEOLOCATION(xxx, yyy), 'mi') < 10 ORDER BY DISTANCE(Location__c, GEOLOCATION(xxx, yyy), 'mi'). I've read that ORDER BY is not supported, but it does work in the VF page.

share|improve this question
add comment

3 Answers 3

I should have known - it is the ORDER BY clause. When I remove the ORDER BY, then the unit test runs fine. But it is strange that the ORDER BY works fine on the VF page.

share|improve this answer
    
The Geolocation SOQL Queries-Beta docs have an example with ORDER BY working: the SELECT and ORDER BY query –  Peter Knolle Nov 28 '12 at 1:42
    
Thanks for pointing that out. I read in another stack overflow post that order by wasn't supported, but I guess it was an older post. –  David Nov 28 '12 at 5:33
add comment

Super helpful post as I couldn't figure out why my deployment was failing..

I couldn't get it deploy with a where clause using geolocation field.

Had to do the following to deploy..

// Don't do geolocation if unit testing as we will bomb in prod deploy
    if (!Test.isRunningTest())
    {
        string qWhere = ' WHERE DISTANCE(Location__c, GEOLOCATION(' + String.valueOf(latitude) + ',' + String.valueOf(longitude) + '),' + '\'mi' + '\') < ' + string.valueOf(radius);
                qWhere += ' AND Id != \''+ centerId.deleteWhitespace() + '\'';

        query += qWhere;
    }
share|improve this answer
add comment

Try clearing your test history. May be then you will be able to have clear view. You can do that by going to Setup > Develop > Apex Test Execution > View Test History > Clear Test Results

share|improve this answer
    
Once again I spoke too fast. After I removed order by, the unit test passed the first time but failed the second time and I could not deploy. I had to remove both DISTANCE references from unit testing in order to pass and deploy. The unit test results are very inconsistent. I have been clicking on the Run Test button on the class screen in the UI and that always fails with the "no such column" error. I've now tried the Apex Test Execution page -- those tests always succeed. In the IDE, the test is successful the first time and then fails the second time. –  David Nov 28 '12 at 16:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.