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.
Attribution to: David
Possible Suggestion/Solution #1
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;
}
Attribution to: Benjamin Pirih
Possible Suggestion/Solution #2
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.
Attribution to: David
Possible Suggestion/Solution #3
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
Attribution to: doga
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4835