[SalesForce] Get address information from Metadata

I would like to get values for Address.Setting Metadata Component in Salesforce.There are stored all country and state ISO codes.

<?xml version="1.0" encoding="UTF-8"?>
<AddressSettings xmlns="http://soap.sforce.com/2006/04/metadata">
    <countriesAndStates>
        <countries>
            <active>true</active>
            <integrationValue>Andorra</integrationValue>
            **<isoCode>AD</isoCode>
            <label>Andorra</label>**
            <orgDefault>false</orgDefault>
            <standard>true</standard>
            <visible>true</visible>
        </countries>
        <countries>
            <active>true</active>
            <integrationValue>Yhdistyneet Arabiemiirikunnat</integrationValue>
            <isoCode>AE</isoCode>
            <label>Yhdistyneet Arabiemiirikunnat</label>
            <orgDefault>false</orgDefault>
            <standard>true</standard>
            <visible>true</visible>
        </countries>

Is there a way to "query" this data, i mean, for example, passing Andorra to get related ISO code "AD".

<countries>
                <active>true</active>
                <integrationValue>Andorra</integrationValue>
                **<isoCode>AD</isoCode>
                <label>Andorra</label>**
                <orgDefault>false</orgDefault>
                <standard>true</standard>
                <visible>true</visible>
            </countries>

I don´t want to turn on the Salesforce Feature named "Country and State picklist".
I want to access with apex code the metadata file that is used to implement this feature(xml above is coming from there).
This metadata file is named:"address.settings"

Do i need to use metadata Api?

How i can accomplish that?

Thanks in advantage for any advice.

Best Answer

Map<String,String> maplabelVal=new Map<String,String>();
// Get the object type of the SObject.
Schema.sObjectType objType = Contact.getSObjectType(); 
// Describe the SObject using its object type.
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();       
// Get a map of fields for the SObject
map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 
// Get the list of picklist values for this field.
list<Schema.PicklistEntry> values = fieldMap.get('MailingCountryCode').getDescribe().getPickListValues();
// Add these values to the selectoption list.
for (Schema.PicklistEntry a : values){ 
    maplabelVal.put(a.getLabel(), a.getValue()); 
}
system.debug('*****'+maplabelVal);
system.debug('ISO CODE'+maplabelVal.get('Andorra'));

You can directly store these with describe calls in MAP as above code


0:06:08:081 USER_DEBUG [14]|DEBUG|*{Afghanistan=AF, Afrique du Sud=ZA, Albanie=AL, Algérie=DZ, Allemagne=DE, Andorra=AD, Angola=AO, Anguilla=AI, Antarctique=AQ, Antigua et Barbuda=AG, ...}

Related Topic