[SalesForce] Is it possible to use the DISTANCE SOQL function without a GEOLOCATION field, or some other approach

I want to make a SOQL call to "near me" filter the ACCOUNT records server-side. (Too many to bring down and filter on the client). It would seem that using DISTANCE function in the call would suit, however presently my ACCOUNT object has no GEOLOCATION field, but it does have Latitude__c and Longitude__c fields. Do I absolutely have to create a GEOLOCATION field on the Account object to use the DISTANCE functionality. Should I do this (then deprecate Lat and Long__c) or should I create custom Apex to do the near me calculation server side, or is there some other easier approach that I have not yet thought of?

Best Answer

It is better strategy to use geolocation field and use distance functionality rather than going for server side painful calculations. Also, it is tricky to do such calculation and is error prone. The hardest part is you have to deprecate the fields that you are having now and run a one time batch job to populate the new field. So, the idea is,

  1. Take a backup of the data
  2. Deprecate the fields
  3. Create geolocation field
  4. Use it in your query or in apex
  5. load the data back in the geolocation fields

Finally, use anything that platform provides is always scalable.

Related Topic