Basically on my scratch org when I create a new field, this one comes prefixed with the namespace.
When I do the same in a packaged environment, the new field is not prefixed.

Why and how can I know on the apex side if a field is prefixed or not ?

1 Answer 1


If you are doing managed package development, the scratch org will need to be created to share that package's namespace.

Anything you then create on that scratch org will naturally be part of that org-wide namespace, allowing you to easily add it to the package.

If you are on an org with a managed package installed, the namespace for that managed package is enclosed within that package - it doesn't apply to the whole org - and the org will typically not have a namespace of its own (though it may if it is a scratch org for extension package development, where it will have the namespace of the extension package for example).

If a field is added to the package (via a scratch org linked to that package's namespace), a package version created and that package installed or upgraded on a "prod" environment then the field will have the package's namespace and is part of that package. However, if you add a field directly on the "prod" environment then clearly it is not part of the package and has no namespace (because the "prod" environment itself has no namespace).

The Apex Schema API includes the DescribeFieldResult which in turn has getName and getLocalName. If these are different the code is being executed in a namespace and the field is part of that same namespace. Otherwise you can simply test to see if the getName value with any "__c" suffix removed (there may not be one) still includes a double underscore to know the field is part of a namespace.

  • Got it for the 2nd part. I did some tests and I should be able to fix my issue. But for the 1st part, I'm sorry it's still blur. I can't even ask clearly what I don't understand
    – learner
    Commented Sep 30, 2021 at 12:19
  • In fact, a packaged app allows easily a developper to deploy, maintain and enhance an app. But why a namespace should not be applied when creating a new field in the "prod" environment ?
    – learner
    Commented Sep 30, 2021 at 12:23
  • Updated to hopefully clarify your latest query.
    – Phil W
    Commented Sep 30, 2021 at 12:28
  • Got it, thanks a lot :)
    – learner
    Commented Sep 30, 2021 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .