Newer Version Available
Address Compound Fields
- Accuracy
- City
- Country
- CountryCode
- Latitude
- Longitude
- PostalCode
- State
- StateCode
- Street
Address fields are provided on many standard objects, such as Account, Contact, Quote, and User. Some objects provide fields for multiple addresses. For example, Account provides for four different addresses. In this case, address field names are prefixed with the type of address, for example, BillingCity, BillingState, and so on.
When an address is geocoded, its latitude and longitude fields are populated with coordinates. A related geolocation field is also populated. Typically, geocoding service providers geocode addresses, and rate the accuracy of the geocodes.
The accuracy subfield GeocodeAccuracy stores the accuracy data for a geocoded location. External geolocation apps can get the accuracy level of a geocoded address via the API. When you retrieve an address via the API, any accuracy data is included. You can also retrieve the accuracy information by itself, if needed.
Like its parent, the compound Address field, the GeocodeAccuracy field is only available for standard address fields on standard objects.
Retrieving Compound Address Fields
1SELECT Name, BillingAddress
2FROM Account1SELECT Name, BillingStreet, BillingCity, BillingState, BillingPostalCode,
2 BillingCountry, BillingLatitude, BillingLongitude
3FROM AccountCompound address field values are returned as a structured data type, Address. Code that works with compound address fields needs to reference the individual components of the returned value. See the code sample below.
Retrieve a Standard Address Compound Field with the SOAP API
The following Java method uses the Salesforce SOAP API to retrieve and display the Mailing Address for a list of contacts.
1// Modified version of code in the SOAP API QuickStart
2private void querySample() {
3 String soqlQuery = "SELECT FirstName, LastName, MailingAddress FROM Contact";
4 try {
5 QueryResult qr = connection.query(soqlQuery);
6 boolean done = false;
7
8 if (qr.getSize() > 0) {
9 System.out.println("\nLogged-in user can see "
10 + qr.getRecords().length + " contact records.");
11
12 while (!done) {
13 System.out.println("");
14 SObject[] records = qr.getRecords();
15 for (int i = 0; i < records.length; ++i) {
16 Contact con = (Contact) records[i];
17 String fName = con.getFirstName();
18 String lName = con.getLastName();
19
20 // Access the compound address field MailingAddress
21 Address addr = (Address) con.getMailingAddress();
22 String streetAddr = "";
23 if (null != addr) streetAddr = addr.getStreet();
24
25 if (fName == null) {
26 System.out.println("Contact " + (i + 1) + ": " + lName +
27 " -- " + streetAddr);
28 } else {
29 System.out.println("Contact " + (i + 1) + ": " + fName +
30 " " + lName +
31 " -- " + streetAddr);
32 }
33 }
34
35 if (qr.isDone()) {
36 done = true;
37 } else {
38 qr = connection.queryMore(qr.getQueryLocator());
39 }
40 }
41 } else {
42 System.out.println("No records found.");
43 }
44 } catch (ConnectionException ce) {
45 ce.printStackTrace();
46 }
47}Using Compound Address Fields as Locations
1SELECT Id, Name, BillingAddress
2FROM Account
3WHERE DISTANCE(BillingAddress, GEOLOCATION(37.775,-122.418), 'mi') < 20
4ORDER BY DISTANCE(BillingAddress, GEOLOCATION(37.775,-122.418), 'mi')
5LIMIT 10