The are a lot of solutions to get the namespace of an specific object or class, e.g.
ApexClass myClass = [
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.
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?