3

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?

1

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.

1

The distance function in SOQL requires that one of the inputs be a geolocation field. But that limitation doesn't apply to distance function in formulas. If all the distances you need to calculate are between two values on the same record, you should be able to use the number fields in a DISTANCE function by wrapping them in the GEOLOCATION() function. Then you could select or sort by the formula results in your SOQL queries.

Example formula:

DISTANCE(GEOLOCATION(Lat1__c,Lon1__c), GEOLOCATION(Lat2__c,Lon2__c),'km')

... where Lat1, Lon1, Lat2, and Lon2 are all number fields.

1

Salesforce rolled out Data.com Clean Rules with their Summer'16 release and what that does is give you the geolocation data for objects with address fields. That includes Accounts, Contacts, and Leads:

Salesforce Data.com Geocode Clean Rules

You can find their knowledge article here.

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.