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();
system.debug(campaignTypeFieldMeta);
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?
Attribution to: jordan.baucke
Possible Suggestion/Solution #1
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).
Edit
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();
System.debug('meta1='+meta1.getType());
System.debug('meta2='+meta2.getType());
System.debug(String.valueOf(meta1.getType()));
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
Attribution to: Daniel Blackhall
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5110