I'm working on a project where I'm communicating with an external billing system to create a new Account in our external billing system when a new Account is created in Salesforce.

Being a billing system, I must include both the Billing and Shipping Addresses (among other information) in the message that I send to the black box, under control of a colleague, that sits between my company's Salesforce org, and our billing system.

Me and my colleague have agreed on using a JSON formatted message for this purpose.

In exploring different methods for constructing my JSON message, I ran across an oddity.

If I query for a composite address field from the Account, either BillingAddress or ShippingAddress, and pass the resulting sObject into JSON.serialize(), the resulting JSON string will contain the expected key/value pair (with the composite address field itself getting serialized).

If I assign the composite address field to a variable of type System.Address and pass that to JSON.serialize(), or if I JSON.serialize(account.BillingAddress), the resulting JSON string is null.

Further, if I have an Apex class which contains a member variable of type System.Address, JSON serialization will work as expected until I populate the System.Address member variable.

Some code to reproduce this behavior

public class ExternalAccount{
    public String name;
    public Address billing_address;

    public ExternalAccount(String inputName, Address inputAddress){
        name = inputName;
        billing_address = inputAddress;

Account acct = [SELECT Id, Name, BillingAddress FROM Account WHERE BillingStreet != null LIMIT 1][0];
ExternalAccount eaWorks = new ExternalAccount(acct.Name, null);
ExternalAccount eaFails = new ExternalAccount(acct.Name, acct.BillingAddress);

system.debug('Serializing sObject directly: ' + JSON.serialize(acct));
system.debug('Serializing BillingAddress directly: ' + JSON.serialize(acct.BillingAddress));
system.debug('Serializing Apex class, null address: ' + JSON.serialize(eaWorks));
system.debug('Serializing Apex class, populated address: ' + JSON.serialize(eaFails));

My question

Why does the JSON class completely croak on being handed System.Address outside of an sObject?

1 Answer 1


If you have access to support, I'd try raising this as a support case to see if it can be handled as a bug.

Failing that, or an official answer from Salesforce, I'd say it is an oversight based on how System.Address was intended to be used. From the Address Compound Fields documentation (my emphasis):

Standard addresses—addresses built into standard objects in Salesforce—are accessible in the SOAP and REST APIs as an Address, a structured compound data type, as well as individual address elements.

I interpret this to mean the System.Address compound type was never intended to be used outside the read only context on an sObject. You can't use it to perform DML, so there wasn't any reason to support it outside of SOQL queries against an sObject.

It appears that you've found an area where that assumption doesn't hold true with JSON serialization. So, try raising it with Salesforce as a bug.

In the short term you could also create your own Address class to hold the required fields. Then use that for serialization. It could include a constructor for the standard System.Address type.

  • 1
    This does appear to be an oversight. There is documentation on using System.Address outside of an sObject, though a variable of this type does need to be set from a compound address field of an sObject to be useful. My org is Sales Cloud - Unlimited edition, so I think I do have access to support. I'll just need to find the time to be able to call in. I did go ahead and create a class to hold the individual address components as well as other required data so I could serialize.
    – Derek F
    Commented Nov 4, 2015 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .