Address Class

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

Namespace

System

Usage

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 addr = myAccount.BillingAddress;
String acctCity = addr.City;

Example

// Select and access Address fields.
// Call the getDistance() method in different ways.
Account[] records = [SELECT id, BillingAddress FROM Account LIMIT 10];
for(Account acct : records) {
   Address addr = acct.BillingAddress;
   Double lat = addr.latitude;
   Double lon = addr.longitude;
   Location loc1 = Location.newInstance(30.1944,-97.6682);
   Double apexDist1 = addr.getDistance(loc1, 'mi');
   Double apexDist2 = loc1.getDistance(addr, 'mi');
   System.assertEquals(apexDist1, apexDist2);
   Double apexDist3 = Location.getDistance(addr, loc1, 'mi');
   System.assertEquals(apexDist2, apexDist3);
}

Address Methods

The following are methods for Address.

getCity()

Returns the city field of this address.

Signature

public String getCity()

Return Value

Type: String

getCountry()

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

Signature

public String getCountry()

Return Value

Type: String

getCountryCode()

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

Signature

public String getCountryCode()

Return Value

Type: String

getDistance(toLocation, unit)

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

Signature

public Double getDistance(Location toLocation, String unit)

Parameters

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.

Return Value

Type: Double

getLatitude()

Returns the latitude field of this address.

Signature

public Double getLatitude()

Return Value

Type: Double

getLongitude()

Returns the longitude field of this address.

Signature

public Double getLongitude()

Return Value

Type: Double

getPostalCode()

Returns the postal code of this address.

Signature

public String getPostalCode()

Return Value

Type: String

getState()

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

Signature

public String getState()

Return Value

Type: String

getStateCode()

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

Signature

public String getStateCode()

Return Value

Type: String

getStreet()

Returns the street field of this address.

Signature

public String getStreet()

Return Value

Type: String