The are a lot of solutions to get the namespace of an specific object or class, e.g.

ApexClass myClass = [
    select NamespacePrefix
    from ApexClass
    where Name = 'SomeClassNameHere'

But in some situations we need something different: the Namespaceprefix of the Current Org as provided here in the setup https://eu5.salesforce.com/0A2 (just replace eu5 with your pod's name).

To clarify: we are NOT looking for a prefix from the scope of an installed managed package. Instead we are looking for the namespaceprefix which the Org itself is (or is not) being providing. That means it's only existent in Developer Editions - never in Production environment. For production systems the result must be always an empty string. The result should be equal when queried from within an installed managed package OR when queried just from unmanaged APEX or via Execute Anonymous.

Why the heck we need this?

if you create Apps focused on Metadata manipulation or inspection, which might be as well installed in Dev-Orgs as in Production Orgs, you don't know this prefix at dev-time. You have to deal with it dynamically on run-time and you need to determine which is the prefix of the Org where you are residing. Again: NOT the one from within which your doing the determination, but the Orgs-one.

Workaround (ugly)

My workaround is - sadly and only out of frustration - to fetch the setup-page and parse the html-body for it. It might break, if Salesforce updates the setup. Also it requires the executing user to have at least read-access to the setup.

I've provided the workaround source code here: How can I get organization's namespace prefix or at least ALL namespace prefixes including org through any API

Obviously, this makes me more than unhappy.

Is there an alternative?

Do you see any alternative to get this Org-Prefix in a better way?

3 Answers 3


A new API for this is documented in the Spring '16 Release Notes.

The Organization object now contains the field NamespacePrefix

Organization org = [SELECT NamespacePrefix FROM Organization];
String ns = org.NamespacePrefix;

You can lean on the System.PageReference API to help a little bit with this:

//gives eg "/apex/ns__apex/"
String qualifiedUrl = new PageReference('/apex/').getUrl();

//gives eg "ns"
return url.substringBefore('__').substringAfterLast('/');
  • Execute from unmanaged Tooling API context to guarantee he works; you could assert(false, result) to make it appear prominently in the response body like a return value. Jul 13, 2015 at 13:02
  • 2
    How awesome - this really looks like providing stable Namespaceprefix results :-) Still testing a bit, all good so far. Thank you so much Matt!!
    – Uwe Heim
    Jul 13, 2015 at 15:00
  • When I call this code in a packaging org I get: 'The name can only contain underscores and alphanumeric characters. It must begin with a letter and be unique, and must not include spaces, end with an underscore, or contain two consecutive underscores.' Jul 28, 2015 at 6:52
  • @RobertSösemann can you try manipulating '/apex/test' instead? Jul 28, 2015 at 7:27
  • 1
    This method does not work for me anymore. Executed on a dev org from code from an installed managed package, it returns NOT the orgs prefix, but the managed package prefix, which is exactly not what I'm looking for.
    – Uwe Heim
    Jan 23, 2016 at 17:01

Do you mean to get the url of the sandbox or production currently user logged in?

then i am using the below to get the url: LEFT($Api.Partner_Server_URL_260, FIND( '/services', $Api.Partner_Server_URL_260))

  • No, the Namespaceprefix has not much to do with the URL (only in some cases the URL is affected a little bit by the NSP). Please read here to understand what it is help.salesforce.com/…
    – Uwe Heim
    Jul 13, 2015 at 14:48

You must log in to answer this question.

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