Address Class
Contains methods for accessing the component fields of address compound fields.
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.
-
Returns the city field of this address.
-
Returns the text-only country/territory name component of this address.
-
Returns the country/territory code of this address if state and country/territory picklists are enabled in your organization. Otherwise, returns
null. -
Returns the distance from this location to the specified location using the specified unit.
-
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.
-
Returns the latitude field of this address.
-
Returns the longitude field of this address.
-
Returns the postal code of this address.
-
Returns the text-only state name component of this address.
-
Returns the state code of this address if state and country/territory picklists are enabled in your organization. Otherwise, returns
null. -
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
Locationto which you want to calculate the distance from the currentLocation. -
unit:
Type: String
The distance unit you want to use:
miorkm.
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 Value | Description |
|---|---|
Address | In the same building |
NearAddress | Near the address |
Block | Midway point of the block |
Street | Midway point of the street |
ExtendedZip | Center of the extended zip code area |
Zip | Center of the zip code area |
Neighborhood | Center of the neighborhood |
City | Center of the city |
County | Center of the county |
State | Center of the state |
Unknown | No match for the address was found |
Geocodes are added only for some standard addresses.
Billing Addresson accountsShipping Addresson accountsMailing Addresson contactsAddresson 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