Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
Nope, just null because it's not in the map :) – LaceySnr Feb 20 at 23:39
1  
System.debug(Schema.GetGlobalDescribe().get('NS__Object__c')) seems to return a value – techtrekker Feb 20 at 23:40
Is that with a managed package? It's definitely not for me... – LaceySnr Feb 20 at 23:44
Yes, Managed Package, however there isn't another custom object with the same name. – techtrekker Feb 20 at 23:45
I don't have one with the same name, but I don't have the prefixed names in the keyset either. Are you testing in the development org for the package? I am and I'm wondering if that's the cause.... – LaceySnr Feb 20 at 23:47
show 3 more comments

4 Answers

up vote 1 down vote accepted

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

share|improve this answer
this will make your describes work in both managed and unmanaged deployments – user320 Feb 21 at 20:11
Unfortunately I'm looking for a dynamic method that can deal with any custom objects - thanks though! – LaceySnr Feb 21 at 21:09
1  
How's String fqName = Type.forName(nsPrefix, 'Virus__c').getName() – user320 Feb 21 at 22:07
Close, but this code might also want to deal with objects in other packages too! That said, there may be a way to grab all the package prefixes... will check it out – LaceySnr Feb 21 at 22:13
Wow! I'm super curious now :-) when does one encounter namespaced objects by their unqualified name, if not inside the packaging org itself? @LaceySnr – user320 Feb 22 at 0:05

Ok, pretty sure this behaviour is because I'm running the code in the development org for the package in question, so if it's already in the right namespace and doesn't need the prefix.

The keys in the map appear to match the values returned by calls to getLocalName() on the corresponding DescribeSObjectResult instances.

share|improve this answer
That's weird... I am definitely seeing NS keys in the getGlobalDescribe() map returned in my DE org under API versions 23-27. – jkraybill Feb 21 at 1:08
For the package you're developing in that org though, or for others? I'm definitely not seeing them :) – LaceySnr Feb 21 at 1:10
For my package, in the DE namespace; getGlobalDescribe keys have my NS in front of them, both in my DE and our production orgs. – jkraybill Feb 21 at 1:12
I'm running it from exec anon in my DE which is Spring '13.... you get the same result there? This is getting very confusing! – LaceySnr Feb 21 at 1:16
whoa... mind blown. keySet() doesn't return namespace at all, but the get() method apparently has magic in it to handle namespaces and case sensitivity!! Schema.getGlobalDescribe().get() returns the same thing for "Foo__c", "foo__c", and "dc__Foo__c"! – jkraybill Feb 21 at 3:23
show 1 more comment

The keySet() method does not return namespaces. However, apparently the getGlobalDescribe().get() method apparently has magic in it to handle namespaces and case sensitivity! In my "dc" namespace, Schema.getGlobalDescribe().get() returns the same thing for "Foo__c", "foo__c", and "dc__Foo__c".

share|improve this answer
This just keeps getting weirder... even if I do that I get null back. Totally stumped. – LaceySnr Feb 21 at 3:57

When populating the global describe map that contains available custom objects, the namespace is stored in lowercase format. So what you need to do is convert the namespace prefix to lowercase and then retrieve the custom object from this map. This is result on my dev org:

Code: Schema.getGlobalDescribe().get('MyNamespace__CustomObject__c');
Result: null

Code: Schema.getGlobalDescribe().get('mynamespace__CustomObject__c'); 
Result: Returns the object 

Also I had encountered an issue, if Schema.getGlobalDescribe() method is used in packaged apex class and the managed package contains an object that has api name identical to an object in the destination organization then this method returns the local object and not the packaged object.

share|improve this answer
It doesn't seem to make a difference for Managed Packages; I was successful both ways. – Mike Chale Feb 21 at 18:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.