Namespace prefix issues with SObject fields map within managed packages

For lots of the cases, we need to find all fields of an SObject (such as Contact) so we do this:

Map<String, SObjectField> CONTACT_FIELDS_MAP = Schema.SObjectType.Contact.fields.getMap();

This returns a map with each key being a field name and value being the corresponding SObjectField. Keys have all lower case characters. Confusion comes in on the keys when this code is executed from within a managed package installed in a customer’s org. An often asked question is whether the key contains namespace prefix (of the managed package) or not. My latest testing shows starting from API v34.0 onward, the map contains both managed package custom fields (with namespace prefix) and local custom fields (without namespace prefix). Prior to this API version, the map only contains fields without namespace prefix so if the local field happens to have the same API name as a managed package field’s, it is overridden by the managed package field. I did the following test to confirm differences between API versions.

In a managed package with namespace prefix, create a simple Apex class with API version set to v33.0:

public class NsTest {
    private static final MapString, SObjectField> CONTACT_FIELDS_MAP = Schema.SObjectType.Contact.fields.getMap();
    public static void test() {
        Map<String, SObjectField> m = DOCUMENT_FIELDS_MAP;
        System.debug('>>> m: ' + m);

Execute NsTest.test() in the developer console and the debug log shows the map contains keys without namespace prefix. Change the class’s API version to v34.0 and re-run the script. The debug log shows the map contains keys with namespace prefix (for custom fields).

Published by Chun Wu

When nothing is certain, anything is possible.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: