Loading
Prepare for Email to Become the Default Login ExperienceRead More
Ongoing maintenance for Salesforce HelpRead More
Extend Salesforce with Clicks, Not Code
DISTANCE

DISTANCE

Calculates the distance between two locations in miles or kilometers.

Use

DISTANCE(mylocation1, mylocation2, 'unit') and replace mylocation1 and mylocation2 with two location fields, or a location field and a value returned by the GEOLOCATION function. Replace unit with mi (miles) or km (kilometers).

Tips

  • The DISTANCE function returns a number data type. Distance is always calculated in decimals, even if you’re displaying the geolocation notation in degrees, minutes, and seconds in the user interface. Specify the number of decimal places to show when you create a custom field.
  • The DISTANCE function isn’t available in reports, but it can be used in list views. To use DISTANCE in your reports, set up a formula field, and then reference the field in your reports.
  • DISTANCE is the only formula function that can use GEOLOCATION parameters.
  • There are limitations on DISTANCE accuracy and equality calculations.
    • DISTANCE supports only the logical operators > and <, returning values within (<) or beyond (>) a specified radius.
    • Distance is calculated as a straight line, regardless of geography and topography between the two points.
    For more details, see “How SOQL Calculates and Compares Distances” in the SOQL and SOSL Reference.
Example: Distance Between Two Geolocation Fields
Example: Distance Between Two Geolocation Fields

DISTANCE(warehouse_location__c, store_location__c, 'mi')

This formula returns the distance, in miles, between the warehouse and the store. In this example, warehouse_location__c and store_location__c are the names of two custom geolocation fields.

Example: Distance Between an Address Field and a Geolocation Field
Example: Distance Between an Address Field and a Geolocation Field

DISTANCE(BillingAddress, store_location__c, 'mi')

This formula returns the distance, in miles, between an account’s billing address and a store. In this example, BillingAddress is the standard billing address field on an Account object, and store_location__c is the name of a custom geolocation field.

Example: Distances with Conditions
Example: Distances with Conditions

IF(DISTANCE(warehouse_location__c, ShippingAddress, 'mi')<10, "Near", "Far")

This formula updates a text formula field to Near if the distance between the warehouse and the account shipping address compound field is less than 10 miles. Otherwise, it updates the text field to Far.

Tip
Tip Although DISTANCE can be calculated in miles or kilometers, the unit isn't returned in the calculation. If possible, include the unit of measure in the name of your distance formula field, so users know whether the distance is in miles or kilometers.
 
Loading
Salesforce Help | Article