I'm using this pattern provided by @bigassforce here How can I get organization's namespace prefix or at least ALL namespace prefixes including org through any API
String qualifiedUrl = new PageReference('/apex/').getUrl(); return url.substringBefore('__').substringAfterLast('/');
This was provided as the "Organization Namespace Prefix", which wrong, but it has provided the Namespaceprefix of the current Managed Package (if executed inside a installed package and '' if executed not inside a MP). This is exactly what I need: not the Orgs-Namespace but that from the package.
Now today I have encountered one single production Org, where it does not work. This line creates an Internal Salesforce Error when run in execute anonymous:
String qualifiedUrl = new PageReference('/apex/').getUrl();
The error goes like this:
Line: -1, Column: -1 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.
The strange thing is, that on a sandbox, spun today this code DOES WORK. The sandbox is not changed after spinning!
Now I'm looking for either a reason, why this code might fail or a alternative code, doing the same but not using any reference to an SObject or anything custom in the org. This code must run inside a utility class (used for various managed and unmanaged code) and therefore I can not assume any custom metadata present - maybe except of the utility class itself.