Address Class

Contains methods for accessing the component fields of address compound fields.

System

Each of these methods is also equivalent to a read-only property. For each getter method, you can access the property using dot notation. For example, myAddress.getCity() is equivalent to myAddress.city.

You can’t use dot notation to access compound fields’ subfields directly on the parent field. Instead, assign the parent field to a variable of type Address, and then access its components. For example, to access the City field in myAccount.BillingAddress, do the following:

“Address” in Salesforce can also refer to the Address standard object. When referencing the Address object in your Apex code, always use Schema.Address instead of Address to prevent confusion with the standard Address compound field. If referencing both the Address object and the Address standard field in the same snippet, you can differentiate between the two by using System.Address for the field and Schema.Address for the object.

The following are methods for Address.

  • getCity()

    Returns the city field of this address.

  • getCountry()

    Returns the text-only country/territory name component of this address.

  • getCountryCode()

    Returns the country/territory code of this address if state and country/territory picklists are enabled in your organization. Otherwise, returns null.

  • getDistance(toLocation, unit)

    Returns the distance from this location to the specified location using the specified unit.

  • getGeocodeAccuracy()

    When using geolocation data for a given address, this method gives you relative location information based on latitude and longitude values. For example, you can find out if the latitude and longitude values point to the middle of the street, instead of the exact address.

  • getLatitude()

    Returns the latitude field of this address.

  • getLongitude()

    Returns the longitude field of this address.

  • getPostalCode()

    Returns the postal code of this address.

  • getState()

    Returns the text-only state name component of this address.

  • getStateCode()

    Returns the state code of this address if state and country/territory picklists are enabled in your organization. Otherwise, returns null.

  • getStreet()

    Returns the street field of this address.

Returns the city field of this address.

public String getCity()

Type: String

Returns the text-only country/territory name component of this address.

public String getCountry()

Type: String

Returns the country/territory code of this address if state and country/territory picklists are enabled in your organization. Otherwise, returns null.

public String getCountryCode()

Type: String

Returns the distance from this location to the specified location using the specified unit.

public Double getDistance(Location toLocation, String unit)

  • toLocation:

    Type: Location

    The Location to which you want to calculate the distance from the current Location.

  • unit:

    Type: String

    The distance unit you want to use: mi or km.

Type: Double

When using geolocation data for a given address, this method gives you relative location information based on latitude and longitude values. For example, you can find out if the latitude and longitude values point to the middle of the street, instead of the exact address.

public String getGeocodeAccuracy()

Type: String

The getGeocodeAccuracy() return value tells you more about the location at a latitude and longitude for a given address. For example, Zip means the latitude and longitude point to the center of the zip code area, in case a match for an exact street address can’t be found.

Accuracy ValueDescription
AddressIn the same building
NearAddressNear the address
BlockMidway point of the block
StreetMidway point of the street
ExtendedZipCenter of the extended zip code area
ZipCenter of the zip code area
NeighborhoodCenter of the neighborhood
CityCenter of the city
CountyCenter of the county
StateCenter of the state
UnknownNo match for the address was found

Geocodes are added only for some standard addresses.

  • Billing Address on accounts
  • Shipping Address on accounts
  • Mailing Address on contacts
  • Address on leads

Person accounts are not supported.

For getGeocodeAccuracy() to work, set up and activate the geocode data integration rules for the related address fields.

Returns the latitude field of this address.

public Double getLatitude()

Type: Double

Returns the longitude field of this address.

public Double getLongitude()

Type: Double

Returns the postal code of this address.

public String getPostalCode()

Type: String

Returns the text-only state name component of this address.

public String getState()

Type: String

Returns the state code of this address if state and country/territory picklists are enabled in your organization. Otherwise, returns null.

public String getStateCode()

Type: String

Returns the street field of this address.

public String getStreet()

Type: String