[SalesForce] Does Schema.GetGlobalDescribe() Account for Prefixes

It seems to me that in my dev org at least, the keys for the map returned by Schema.GetGlobalDescribe() do not use namespace prefixes… so does anybody know what happens when there are two objects with the same name in two different namespaces? It seems to me like there may be a bug here.

If I try and look up with Schema.GetGlobalDescribe().get('MyNamespace__MyObject__c') then I get no hits, but if I do Schema.GetGlobalDescribe().get('MyObject__c').getDescribe().getName() then I get the name with the prefix, so I'd expect to use that same name to perform lookups in the first case.

Best Answer

Does Schema.GetGlobalDescribe() Account for Prefixes?

Yep! Use SObjectType.Obj__c.Name to resolve the namespace, for example:

Static method:

String fullyQualifiedName = SObjectType.Obj__c.Name;
System.debug(fullyQualifiedName);
//ns__Obj__c

String label = Schema.GetGlobalDescribe().get(fullyQualifiedName).getDescribe().getLabel();
System.debug(label);
//Obj

Edit: dynamic method

Replace line 1: String fqName = Type.forName(isPkg ? 'ns' : '', 'Obj__c').getName() where ns is set only in the packaging org, eg via Protected Custom Setting

Related Topic