img1

In Apex, all variables and expressions have a data type, such as sObject, primitive, or enum. Compound fields group together multiple elements of primitive data types, such as numbers or strings, to represent complex data types, such as a location or an address. Compound fields are an abstraction that can simplify application code that handles the values, leading to more a concise, understandable code. In laymen terms Compound data types are primitives grouped to form a new type. They effectively simplify application code, as you can reference one data type rather than each of the primitives that comprise the compound field.

Nice to know

  • Compound fields are accessible as a single, structured field, or as individual component fields.
  • The values contained within the compound field and the values in individual fields both map to the same underlying data stored in Salesforce; they always have identical values.
  • Any code that references individual component fields is unaffected by the new compound fields.
  • Compound fields are read-only.
  • Compound fields are available only through the SOAP and REST APIs.

Compound Data Types

  • Address
  • Geolocation

Geolocation Compound Field

It is a location-based structured compound type, containing individual latitude and longitude values. You can use one, by creating a Geolocation field for an object of interest.

  • A geolocation compound field is read-only, although its latitude and longitude subfields are editable.
  • Although geolocation fields appear as a single field in the user interface, custom geolocation fields count as three custom fields towards your organization’s limits: one for latitude, one for longitude, and one for internal use.

Address Compound Fields

This compound data type is also a structured one, containing all the standard address fields for a location.

  • Accuracy, Accuracy level of the geocode for the address.
  • City
  • Country
  • CountryCode, The ISO country code for the address.
  • Latitude
  • Longitude
  • PostalCode
  • State
  • StateCode, The ISO state code for the address
  • Street

Single address compound field access

1
2
SELECT Name, BillingAddress
FROM Account

Multiple address field access

1
2
3
4
SELECT Name, BillingStreet, BillingCity, 
BillingState, BillingPostalCode, BillingCountry, 
BillingLatitude, BillingLongitude
FROM Account

Compound Field Considerations, Limitations and Implications

  • Compound fields are read-only. To update field values, modify the individual field components
  • Although compound fields can be queried with the Location and Address Apex classes, they’re editable only as components of the actual field. Read and set geolocation field components by appending “latitudes” or “longitudes” to the field name, instead of the usual “__c.”
1
2
Double theLatitude = someObjbect.Location__latitude__s;
someObjbect.Location__longitude__s = -43.44;
  • You can’t use compound fields in Visualforce pages, for example, in an apex:outputField. To access or update field values, use the individual field components.
  • Compound addresses are only available on certain standard objects (Account, Lead, Quote etc). You cannot create your custom address compound fields.
  • If you select compound fields for export in the Data Loader, they cause error messages. To export values, use individual field components.
  • Custom geolocation and location fields on standard addresses aren’t supported with email templates.
  • You can’t use compound fields in lookup filters, except to filter distances that are within or not within given ranges. You can use distance lookup filters only in the Metadata API.
  • The only formula functions that you can use with compound fields are ISBLANK, ISCHANGED, and ISNULL. You can’t use BLANKVALUE, CASE, NULLVALUE, PRIORVALUE, or the equality and comparison operators with compound fields.
  • In Developer, Professional, Enterprise, Unlimited, and Performance editions, Salesforce can automatically add or update geolocation fields for Account, Contact, Lead, and WorkOrder records. To use this feature, your administrator must enable the geo data integration rule for each object. For all other objects and editions, set values for latitude and longitude by using SOQL, Workbench, SOAP or REST API, or a geocoding service. You can then use address fields as locatable values.

Address Limitations

  • Compound address fields are available only for address fields that exist as part of the standard objects included in Salesforce. You can’t create custom compound address fields.
  • The accuracy subfield of address fields is populated only when an address is geocoded.
  • Address fields can’t be used in WHERE statements in SOQL. Address fields aren’t filterable, but the isFilterable() method of the DescribeFieldResult Apex class erroneously returns true for address fields.

Geolocation Limitations

  • Geolocation fields aren’t supported in custom settings.
  • Geolocation fields aren’t available in dashboards or Schema Builder.
  • Geolocation fields are available in Visual Workflow and formula-based workflow and approvals, but they can’t be used in filter-based workflow updates and approvals.
  • Geolocation fields are supported in SOQL with the following limitations
    • DISTANCE and GEOLOCATION are supported in WHERE and ORDER BY clauses in SOQL, but not in GROUP BY. DISTANCE is supported in SELECT clauses
    • DISTANCE supports only the logical operators > and <
    • When using the GEOLOCATION function in SOQL queries, the geolocation field must precede the latitude and longitude coordinates. For example, DISTANCE(warehouse_location__c, GEOLOCATION(37.775,-122.418), 'km') works but DISTANCE(GEOLOCATION(37.775,-122.418), warehouse_location__c, 'km') doesn’t work.
    • Apex bind variables aren’t supported for the units parameter in the DISTANCE function. This query doesn’t work.
1
2
3
4
5
String units = 'mi';
List<Account> accountList = 
    [SELECT ID, Name, BillingLatitude, BillingLongitude 
     FROM Account 
     WHERE DISTANCE(My_Location_Field__c, GEOLOCATION(10,10), :units) < 10];