13

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 the DescribeSObjectResult.fields.getMap() method.

Current Result

Only local custom fields are returned (e.g. Description__c). Using Salesforce API version 29.0

How to reproduce

  1. Create a custom object / custom field (e.g. namespace__Object__c.namespace__Description__c) and upload the managed package.
  2. Install the managed package in a different organization (e.g. a developer edition organization)
  3. In the custom object namespace__Object__c add a new custom field named Description__c (therefore it doesn't have a namespace)
  4. 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 and zqu__Description__c fields.

7

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?

  • 1
    Running the code from within the namespace is the issue indeed. There is only one reference to that API name (without namespace) in the map. What is strange is that Salesforce then add the namespace prefix to the other fields (in the same namespace) before returning the DescribeSObjectResult object. Which is what confused me, since I saw the local namespace field Description__c and all the managed fields like N1__CustomField__c in the returned map (so I was assuming the missing field should not have been overriden). – Mickael Feb 18 '14 at 19:09

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.