1

As I read the documentation we should be able to get Address Settings with a readMetadata request to the Metadata API endpoint (https://SF_INSTANCE.salesforce.com/services/Soap/m/33.0), but in my test no results are returned. Is reading from the AddressSettings with the readMetadata call actually not supported? The metadata documentation indicates that AddressSettings are available starting in API 33.

This request works for other Setting types SecuritySettings, AccountSettings, BusinessHoursSettings, etc.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:met="http://soap.sforce.com/2006/04/metadata">
   <soapenv:Header>
      <met:SessionHeader>
         <met:sessionId>YOUR_SESSION_ID</met:sessionId>
      </met:SessionHeader>
   </soapenv:Header>
   <soapenv:Body>
      <met:readMetadata>
         <met:type>AddressSettings</met:type>
         <met:fullName>*</met:fullName>
      </met:readMetadata>
   </soapenv:Body>
</soapenv:Envelope>

Reply:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <readMetadataResponse>
         <result>
            <records xsi:nil="true"/>
         </result>
      </readMetadataResponse>
   </soapenv:Body>
</soapenv:Envelope>

To call directly from APEX (don't forget to add the remote site setting for the endpoint)

HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml');
req.setCompressed(false);
req.setHeader('SOAPAction', 'readMetadata');
String sessionId = UserInfo.getSessionId();
System.debug('SESSIONID: ' + UserInfo.getSessionId());
String body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:met="http://soap.sforce.com/2006/04/metadata"><soapenv:Header><met:SessionHeader>';
body += '<met:sessionId>' + sessionId + '</met:sessionId>';
body += '</met:SessionHeader></soapenv:Header><soapenv:Body>';
body += '<met:readMetadata><met:type>AddressSettings</met:type><met:fullName>*</met:fullName></met:readMetadata>';
body += '</soapenv:Body></soapenv:Envelope>';
req.setBody(body);
req.setEndpoint('https://SF_INSTANCE.salesforce.com/services/Soap/m/33.0');
HTTPResponse resp = h.send(req);
System.debug(resp.getBody());
1

The fullName parameter needs to be "Address" it does not take the * wildcard like the other Settings objects.

Updated working SOAP request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:met="http://soap.sforce.com/2006/04/metadata">
    <soapenv:Header>
          <met:SessionHeader>
             <met:sessionId>YOUR_SESSION_ID</met:sessionId>
          </met:SessionHeader>
       </soapenv:Header>
       <soapenv:Body>
          <met:readMetadata>
             <met:type>AddressSettings</met:type>
             <met:fullName>Address</met:fullName>
          </met:readMetadata>
       </soapenv:Body>
    </soapenv:Envelope>

Updated Apex callout:

HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml');
req.setCompressed(false);
req.setHeader('SOAPAction', 'readMetadata');
String sessionId = UserInfo.getSessionId();
System.debug('SESSIONID: ' + UserInfo.getSessionId());
String body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:met="http://soap.sforce.com/2006/04/metadata"><soapenv:Header><met:SessionHeader>';
body += '<met:sessionId>' + sessionId + '</met:sessionId>';
body += '</met:SessionHeader></soapenv:Header><soapenv:Body>';
body += '<met:readMetadata><met:type>AddressSettings</met:type><met:fullName>Address</met:fullName></met:readMetadata>';
body += '</soapenv:Body></soapenv:Envelope>';
req.setBody(body);
req.setEndpoint('https://SF_INSTANCE.salesforce.com/services/Soap/m/33.0');
HTTPResponse resp = h.send(req);
System.debug(resp.getBody());
0

The answer is available on this thread Get address information from Metadata

Once country and state picklists is enabled in the org, you could simply access all the values from the picklist values of those picklist field from account or contact object. The link has the example for getting it via apex, if you need the same info via REST API then use the /services/data/v29.0/sobjects/Account/describe or /services/data/v29.0/sobjects/Contact/describe endpoints and parse the picklist values for the statecode or countrycode fields.

  • Thanks Rajiv, I've seen the that thread. My question is specific to the seeming missing functionality of the readMetadata method for the AddressSettings object. – Carl Weiss Apr 28 '15 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.