Here's what I'm trying to do:

  1. Search and find a list of contact records with zip codes.
  2. Filter those records using zip codes that are in 5 km radius.

I'm unable to perform the 2nd point.

Is there any document which could help me in this?

  • 1
    Could you not use standard Salesforce "as the crow flies" (aka great circle) distance measures, or do you need street level routing distance? – Phil W Jan 17 at 9:59
  • I would only have zipcode to filter the contacts. Yes, the 5km radius would be my criteria to filter. Would it be necessary to use google api or is there an out of the box feature. – MnZ Jan 17 at 10:04

SOQL supports Location-Based Queries. You can retrieve records with locations within or outside of a certain radius with DISTANCE conditions in the WHERE clause of the query.

You can combine Distance and ZIP Code list to get the desired contacts.

Your SOQL should look this:

List<Contact> contactList = [Select Id, FirstName, LastName From Contact Where Distance(MailingAddress,GeoLocation(:latitude, :longitude),'km') < 5 AND MailingPostalCode IN :zipCodeList];
| improve this answer | |
  • Thanks @saroj-bera. I'm confused from the salesforce documentation of Location-Based Queries. Is the MailingAddress and GeoLocation(:latitude, :longitude) on the same record? I would need all the contacts within 5 kms. Any idea on that? – MnZ Jan 24 at 12:51
  • @MnZ MailingAddress is for contact record and GeoLocation(:latitude, :longitude) is for the location from where you are looking for the contacts. – Saroj Bera Jan 24 at 13:12
  • So do u mean "San Fransico, California" is the MailingAddress for the contact "Tom" and GeoLocation(123.123, 456.456) is the location from where the radius of 5 km will be searched? Is my understanding correct? – MnZ Jan 27 at 17:21

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.