12

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.

  • Nope, just null because it's not in the map :) – Matt Lacey Feb 20 '13 at 23:39
  • 1
    System.debug(Schema.GetGlobalDescribe().get('NS__Object__c')) seems to return a value – techtrekker Feb 20 '13 at 23:40
  • Is that with a managed package? It's definitely not for me... – Matt Lacey Feb 20 '13 at 23:44
  • Yes, Managed Package, however there isn't another custom object with the same name. – techtrekker Feb 20 '13 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.... – Matt Lacey Feb 20 '13 at 23:47
5

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

  • this will make your describes work in both managed and unmanaged deployments – bigassforce Feb 21 '13 at 20:11
  • Unfortunately I'm looking for a dynamic method that can deal with any custom objects - thanks though! – Matt Lacey Feb 21 '13 at 21:09
  • 1
    How's String fqName = Type.forName(nsPrefix, 'Virus__c').getName() – bigassforce Feb 21 '13 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 – Matt Lacey Feb 21 '13 at 22:13
  • 1
    Wow! I'm super curious now :-) when does one encounter namespaced objects by their unqualified name, if not inside the packaging org itself? @LaceySnr – bigassforce Feb 22 '13 at 0:05
6

I found that the describe map was only partially case-insensitive:-

Schema.getGlobalDescribe().get('CustomObject__c');    // works
Schema.getGlobalDescribe().get('customobject__c');    // works
Schema.getGlobalDescribe().get('ns__CustomObject__c');    // works
Schema.getGlobalDescribe().get('Ns__CustomObject__c');    // doesn't work

So object names are case-insensitive, but namespaces have to be lower-case - even though I have a mixed-case namespace. That's why:-

Schema.getGlobalDescribe().get(objectName.toLowerCase());

can be used as a workaround.

  • 1
    Great work man, that explains a lot!! – Matt Lacey Nov 22 '13 at 19:48
3

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.

  • It doesn't seem to make a difference for Managed Packages; I was successful both ways. – Mike Chale Feb 21 '13 at 18:25
2

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.

  • 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 '13 at 1:08
  • For the package you're developing in that org though, or for others? I'm definitely not seeing them :) – Matt Lacey Feb 21 '13 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 '13 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! – Matt Lacey Feb 21 '13 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 '13 at 3:23
2

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm

Starting with 28.0 API version describe will have namespace included in the map key

  • 1
    This answer is accurate as of the latest version of the API. Note that older code, 27.0 and below, cannot access non-namespaced objects that share the same name as an object within its namespace. MyObject__c in a managed package, with the org also having a MyObject__c, will render the org's version unreachable from managed code. Conversely, unmanaged code in the org will be able to see both versions. – sfdcfox Jul 6 '13 at 2:13
1

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".

  • This just keeps getting weirder... even if I do that I get null back. Totally stumped. – Matt Lacey Feb 21 '13 at 3:57
0

I have also faced the same problem while getting describe results after installing the package. I tried with the following code :

Code:

Schema.getGlobalDescribe().get('CustomObject__c'); 

Result: Returns the object

No need to give name prefix while getting result from describe call.

  • Welcome to salesforce.stackoverflow. Is it possible that you intended to comment on the question rather than give an answer ? On Stackoverflow sites the answer functionality isn't intended to be used like replies on boards. – Samuel De Rycke Jun 4 '13 at 8:58
0

So to chip in with yet another seemingly working answer, I have the name of the object set ast the objectName variable so it can be any object in the system but for what I am working on, is an object in a managed package where I am working in the packaging org.

Schema.getGlobalDescribe().get(objectName.toLowerCase())

This gives out the correct data for the object whereas just objectName itself didn't work.

Very strange still and hope it helps someone as none of the above were working for me in my scenario.

Paul

  • The keys are lowercase, but the map retrieved from "getglobaldescribe" is case insensitive. This is a special exception to normal maps, which are case sensitive. get('MyObject__c') and get('myobject__c') are both correct. – sfdcfox Jul 6 '13 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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