I am trying to use the APEX method: .getType() field metadata like so:

Schema.DescribeFieldResult campaignTypeFieldMeta = null;
Map<String,Schema.SObjectField> mapFields = Campaign.sObjectType.getDescribe().fields.getMap();
campaignTypeFieldMeta = mapFields.get('NS__CustomFieldName__c').getDescribe();
String typeString = String.ValueOf(campaignTypeFieldMeta.getType())

This works normally in an unpackaged org, however, when I get into my packaging org, the debug statement says: "Entering Managed Package" in the debug log. (This is my packaging org, not where the package is installed, so I don't know why I should be able to see my data own code execution!)

Furthermore, the last line in my code throws a 'Attempt to dereference a null object' exception.

String typeString = String.ValueOf(campaignTypeFieldMeta.getType()) // throws null exception in packaging org.

Is this something I'm misunderstanding about metadata describes?


I have witnessed this first hand as well. The problem is that only in a Packaging org this call:

Map<String, Schema.SObjectField> mapFields = Campaign.sObjectType.getDescribe().fields.getMap();

The Map keys don't have the namespace! But when run by a subscriber the Map keys are correctly namespaced field names.

This is why you're getting a null pointer campaignTypeFieldMeta is null because the Map doesn't contains the key NS__CustomFieldName__c changing to mapFields.get('CustomFieldName__c') will work in your packaging org but will break for your subscribers

My unintended solution was to write a Util method for returning the namespace of the org the code is running in (this was for development reasons not for this issue).


Further to your comment I went back and tested this, and got the following results.

Map<String,Schema.SObjectField> mapFields = Campaign.sObjectType.getDescribe().fields.getMap();
Schema.DescribeFieldResult meta1 = mapFields.get('CustomFieldName__c').getDescribe();
Schema.DescribeFieldResult meta2 = mapFields.get('NS__CustomFieldName__c').getDescribe();

And got the following from the debug log.

17:07:54:032 USER_DEBUG [4]|DEBUG|meta1=DOUBLE
17:07:54:032 USER_DEBUG [5]|DEBUG|meta2=DOUBLE
17:07:54:033 USER_DEBUG [6]|DEBUG|DOUBLE

So what I said about namespaced map keys is no longer the case, both a namespaced and non namespaced key are in the map.

As for your problem with Entering Managed Package I was unable to replicate that. I was running anonymously from the developer console, what about you?

Last follow up, what was the field type of CustomFieldName__c, because I suspect that is a separate issue

  • I'm gonna give it too you Daniel 'cause I wasn't altogether sure I wasn't doing something stupid, so it's not even getting the key out of the map? Cause when I run debug I see it with the namespace preview in the map?? – jordan.baucke Dec 6 '12 at 3:47
  • @jordan.baucke I've added a major edit, the summary is: I can't replicate your issue in my packaging org. – Daniel Blackhall Dec 6 '12 at 6:17
  • @jordan.baucke sorry to dredge this question up, I am working on a different product in a different packaging org, and I am getting the ENTERING_MANAGED_PKG in the debug logs. Were you able to resolve this? – Daniel Blackhall Dec 12 '12 at 23:46
  • I wasn't no. Ultimately, it was decided that the code that was problematic wasn't needed per the headache we were having getting metadata about that particular field. If my memory serves though, we were supplying the proper NS__ prefix on the field, and still getting a null result so I think there might be a bug there. – jordan.baucke Dec 13 '12 at 2:24

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.