New Schema Method for Describing Multiple sObjects

If you are building ISV packages on Force.com, you quickly end up needing to do Describe calls on sObjects. Prior to Winter 14, you could either request the Describe results for a single sObject, or get the describe results for all sObjects. Most people then end up writing utility classes to cache those results into a static Map so you could retrieve the results from a Map as you needed them, and not breach any describe call limits.

But in Winter 14, there is a new Schema Method which let’s you pass in a string [] of sObjects names you want describe results for, and get back an Schema.DescribeSobjectResult [] for the results, which will prove really helpful when you need the results for two or three sObjects and a full describe seems like overkill. As ever with new Force.com releases, I also now get to go back and update all those utility classes I built/borrowed from others…

Here’s the sample code from the docs

// sObject types to describe
String[] types = new String[]{'Account','Merchandise__c'};
// Make the describe call
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(types);
System.debug('Got describe information for ' + results.size() + ' sObjects.');
// For each returned result, get some info
for(Schema.DescribeSobjectResult res : results) {
System.debug('sObject Label: ' + res.getLabel());
System.debug('Number of fields: ' + res.fields.getMap().size());
System.debug(res.isCustom() ? 'This is a custom object.' : 'This is a standard object.');
// Get child relationships
Schema.ChildRelationship[] rels = res.getChildRelationships();
if (rels.size() > 0) {
System.debug(res.getName() + ' has ' + rels.size() + ' child relationships.');
}
}

Leave a Reply