Yes, you just pull the SObjectType
out of the global describe.
SObjectType accountType = Schema.getGlobalDescribe().get('Account');
DescribeSObject accountDescribe = accountType.getDescribe();
Note that calling getGlobalDescribe()
is quite CPU intensive and should be avoided in any sort of loop. Typically, if I expect to be retrieving a high volume of describes, I still implement a DescribeCache
to save on this limit. In addition to performance, it also facilitates mocking if you make the class/methods virtual. It typically looks something like below:
public with sharing class DescribeCache
{
static Map<String, SObjectType> objects = Schema.getGlobalDescribe();
public static SObjectType getSObjectType(String objectName)
{
return objects.get(objectName);
}
static Map<SObjectType, Map<String, SObjectField>> fields =
new Map<SObjectType, Map<String, SObjectField>>();
public static SObjectField getField(String objectName, String fieldName)
{
return getField(getSObjectType(objectName), fieldName);
}
public static SObjectField getField(SObjectType sObjectType, String fieldName)
{
if (sObjectType == null) return null;
if (!fields.containsKey(sObjectType))
fields.put(sObjectType, sObjectType.getDescribe().fields.getMap());
return fields.get(sObjectType).get(fieldName);
}
}