Problem Description
Not being able to use both custom and managed fields in a managed package when fields share the same API name (with different namespaces). When using the
DescribeSObjectResult.fields.getMap()
method, it only returns the local custom field (and not both the local custom field and the managed field).Expected Result
Map of with both the local custom field (e.g.
Description__c
) and managed field (e.g.namespace__Description__c
) returned by theDescribeSObjectResult.fields.getMap()
method.Current Result
Only local custom fields are returned (e.g.
Description__c
).
Using Salesforce API version 29.0How to reproduce
- Create a custom object / custom field (e.g.
namespace__Object__c.namespace__Description__c
) and upload the managed package.- Install the managed package in a different organization (e.g. a developer edition organization)
- In the custom object
namespace__Object__c
add a new custom field namedDescription__c
(therefore it doesn’t have a namespace)- Run the following code from the managed package
(You can/should embed it in a VF page / custom Apex controller for easy access):
Schema.DescribeSObjectResult dsr = Schema.getGlobalDescribe().get('zqu__Quote__c').getDescribe(); Map objectFields = dsr.fields.getMap(); for (String fieldName : objectFields.keySet()) { Schema.DescribeFieldResult dfr = objectFields.get(fieldName).getDescribe(); System.debug(dfr.getName() + ' >> ' + dfr.getLabel()); }
When running in the managed package, this code will only return the customer local
Description__c
custom field.However, when running this code from the Developer Console in the local namespace, this code will return both
Description__c
andzqu__Description__c
fields.
Answer
The way I read this line in the docs, that is expected behavior:
For example, if the code block that generates the map is in namespace
N1, and a field is also in N1, the key in the map is represented as
MyField_c. However, if the code block is in namespace N1, and the
field is in namespace N2, the key is N2_MyField__c.
So if you are running the code within your namespace, the key is created without the namespace prefix. Since a field created directly in the target Org won’t have a namespace either, when the map is generated it will only have one reference to that api name in the map. The only way I can see to avoid that is to add a prefix to the API name for your fields in addition to the namespace, so any custom fields in your package will still be identifiable. Defeats the objective of a namespace, but based on the docs, not sure what else you can do?
Attribution
Source : Link , Question Author : Mickael , Answer Author : BritishBoyinDC