How to access Compound field values in Apex Salesforce

What is compound field?

Compound field – for example Address and Geolocation – binds together multiple information into a single unit. Like Address is one unit of information but actually it is comprises of different primitive information like Street Address, City, State, Country & Postal Code.

Can we query/access address field without addressing individual field components?

Yes, as below.

for(Account acc : [SELECT Name, BillingAddress FROM Account LIMIT 10]){
    Address addr = (Address) acc.BillingAddress;
    if(addr!=null){    {
        System.debug('Address Value: '+addr);
        System.debug('# City '+addr.getCity());
        System.debug('# Country '+addr.getCountry());
Can we update compound field directly without going further in apex?

Compound fields are read-only. To update field values, modify the individual field components.

Want to learn more?