Extracting Schemas from SalesForce

Nerding on November 26th, 2008 2 Comments

It took me way too long to figure out how to get SalesForce to export schema information on an object.  It seemed so tantalizingly easy – the salesforce.schema file is right there in the IDE but it’s a placeholder for a dynamically updated widget…that won’t export the XML it clearly contains.

So, I learned about Maps, SObjectFields, and some other stuff.  I might have even helped this person out, who posted today about nearly the same thing.

Map<string,Schema.SObjectField> M =
  Schema.SObjectType.Organization_Detail__c.fields.getMap();
Set s = M.keySet();
for (String s1 : s) {
  SObjectField f = M.get(s1);
  Schema.DescribeFieldResult d = f.getDescribe();
  System.debug(logginglevel.INFO,d.getName() + ',' + d.getLabel());
}

Execute the above as anonymous, and you should get a comma separated set of names and labels.  I’m in no mood to figure out how to suppress the 20081127030828.685:AnonymousBlock: line 6, column 3: that it poops out, so I’ll trim it down in TextPad.

Tags: , , ,

2 Responses to “Extracting Schemas from SalesForce”

  1. Jeff says:

    I get the following error when I run the code in Eclipse:
    Compile error at line 3 column 4
    expecting a semi-colon, found ‘s’
    Any ideas?

  2. Ehren says:

    Did you change the < and > to < and >? Also there have been a few apex version releases since this was written.

Leave a Reply