1

So I was planning to use SOQL geolocation to fetch the nearest dealer to the location of our agent. To find nearest I am trying to use Order by clause along with geolocation. The SOQL is below

 List<Account> dealerShipAccount=   [SELECT id,name,Vienna_Legacy_Id__c FROM Account WHERE (RecordTypeId=:ACC_DEALRESHIP_RECORDTYPEID AND
                                DISTANCE(ShippingAddress,GEOLOCATION(:opp.GEOLOCATION__Latitude__s,:opp.GEOLOCATION__Longitude__s),'mi') < :radius ) 
                                ORDER BY DISTANCE(ShippingAddress,GEOLOCATION(:opp.GEOLOCATION__Latitude__s,:opp.GEOLOCATION__Longitude__s),'mi') 
                                LIMIT 1];

When I try to save it via Metadata/Tooling API or via SF Edit class UI I get GAcK.

An internal server error has occurred An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience.

Thank you again for your patience and assistance. And thanks for using salesforce.com!

Error ID: 1358189510-219274 (-517776265)

When I remove the Order by clause the SOQL

List<Account> dealerShipAccount=    [SELECT id,name,Vienna_Legacy_Id__c FROM Account WHERE (RecordTypeId=:ACC_DEALRESHIP_RECORDTYPEID AND
                                    DISTANCE(ShippingAddress,GEOLOCATION(:opp.GEOLOCATION__Latitude__s,:opp.GEOLOCATION__Longitude__s),'mi') < :radius ) 
                                    LIMIT 1];

I cant find this in Known Issue section, can anyone throw some light whats wrong.

  • Have you try adding a n null check for Latitude fields in where clause? – Raul Nov 6 '17 at 11:15
  • Its compile time error, so I believe it should be irrelevant. – Pranay Jaiswal Nov 6 '17 at 11:20
  • did you try to use an alias for the distance expression, not sure if it can be used in Order by though.. – Basti Nov 6 '17 at 11:23
  • @Basti sorry mate, didnt work – Pranay Jaiswal Nov 6 '17 at 11:46
2

So I raised a case with Salesforce and got the below response.

We have heard back from R&D and Tier-3 .

The issue is due a bug with old compiler , there is a workaround which you could implement in your org .

The workaround is instead of directly assigning sobject field paths to the geolocation function, once assigning the field paths to single reference local variables, pass them to the geolocation function, as follows.


public static void findNearbyDealers(Opportunity opp){ 
double latitude = opp.GEOLOCATION__Latitude__s; 
double longitude = :opp.GEOLOCATION__Longitude__s; 
List dealerShipAccount=[Select id,name from Account where 
DISTANCE(ShippingAddress,GEOLOCATION(:latitude, :longitude),'mi') < 20 
order by DISTANCE(ShippingAddress,GEOLOCATION(:latitude, :longitude),'mi') 
limit 1]; 
} 
}

I tried the solution provided by them, and it works charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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