1

I try to programmatically get the fields and their types of all Salesforce Objects (including custom ones). But some Types don't seem to exist.

For this i use the force.com wsc version 32.0.0, the partner wsdl and the metadata wsdl.

public FileProperties[] listMetadata(String type){
    FileProperties[] props = null;
    ListMetadataQuery query = new ListMetadataQuery();
    query.setType(type);
    try {
      props = connector.getConnection().listMetadata(new ListMetadataQuery[]{query}, API_VERSION);
    } catch (ConnectionException e) {
      LOG.warn("Exception occured while trying to list Metadata for {}.", type, e);
    }
    return props;
  }

I grab the list of Metadata (the parameter is 'CustomObject'), then I try to get each object separately to get the fields.

public CustomObject getObjectByName(String name) {
    CustomObject object = new CustomObject();
    try {
      ReadResult result = connector.getConnection().readMetadata("CustomObject", new String[] { name });
      Metadata[] metadata = result.getRecords();
      object = (CustomObject) metadata[0];
    } catch (ConnectionException e) {
      LOG.warn("ConnectionException occured while trying to read Metadata. Custom Object Name: {}", name, e);
      object.setFullName(name);
    }
    return object;
  }

listing objects:

public void getFieldsForObjects() {
    System.err.println("=============== object with fields ===============");

    FileProperties[] props = reader.listMetadata("CustomObject");
    for (FileProperties prop : props) {
      CustomObject o = null;
      o = reader.getObjectByName(prop.getFullName());
      printObject(o);
    }
  }

and then I get:

Opportunity {
    AccountId : Lookup
    Amount : null
    CampaignId : Lookup
    CloseDate : null
    ContractId : Lookup
    CurrentGenerators__c : Text
    DeliveryInstallationStatus__c : Picklist
    Description : null
    ExpectedRevenue : null
    IsPrivate : null
    LeadSource : Picklist
    MainCompetitors__c : Text
    Name : null
    NextStep : null
    OrderNumber__c : Text
    OwnerId : Lookup
    Probability : null
    StageName : Picklist
    TotalOpportunityQuantity : null
    TrackingNumber__c : Text
    Type : Picklist
}

Can anyone tell why it is like that and what to do? I already checked both wsdls and they don't seem to have the types declared. Missing types are currency, percent, and some others.

1 Answer 1

0

I try to programmatically get the fields and their types of all Salesforce Objects (including custom ones)

If this is your goal then the Partner API should be sufficient using the various describe calls.

You could start by calling describeGlobal() and looping through the DescribeGlobalResult to get all the API names of the available objects.

Then use describeSObjects() to get the list of fields for a collection of sObjectTypes. Field has a type property that is a FieldType. That definitely has currency, percent, and all the other types used for fields via the API.

Note that some types you may encounter in Apex don't have a corresponding wire type. E.g. Decimal will be send as a Double or Currency instead.


Here is an example of the partner sourced meta data for Opportunity: enter image description here

1
  • Thanks a lot. That one really helped. I also discovered that it is possible to get the field types via the REST-API.
    – Marco Gora
    Commented Feb 19, 2015 at 12:42

You must log in to answer this question.

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