5

I'm using this pattern provided by @mattandneil 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.

3
  • Do you know when this org was set up? I'm also getting that error in my DE org. It was created a long long time ago.
    – Nick C
    Commented Sep 13, 2016 at 13:48
  • This issue only shows up on very new Orgs for us. I would say that these Orgs have been spun within the last couple of weeks. I noticed that miss behavior today the first time.
    – Uwe Heim
    Commented Sep 13, 2016 at 14:30
  • Did you get anything working?
    – Adrian Larson
    Commented Sep 23, 2016 at 23:23

3 Answers 3

3

I haven't tried installing it anywhere, but the following works in a packaging org for me:

public class MyUtil
{
    class CustomException extends Exception { }
    public static String getNamespacePrefix()
    {
        String prefix;
        try { throw new CustomException(); }
        catch (CustomException e)
        {
            prefix = e.getStackTraceString().substringBetween('(', ')');
        }
        return prefix;
    }
}

The entire stack trace string for me is simply (prefix), so getting the content between parens gives me prefix.

0

I found this code here only using the classname itself

public String getNamespacePrefix() {
    String namespacePrefix;
    String[] classNameParts = String.valueOf(<Enter className>.class).split('\\.', 2);
    if(classNameParts.size() > 1) {
        Type classType = Type.forName(classNameParts[0], classNameParts[1]);
        if(classType == <Enter className>.class) {
            namespacePrefix = classNameParts[0];
        } else {
            namespacePrefix = '';
        }
    } else {
        //If there is only one part, the class has no namespace
        namespacePrefix = '';
    }
    return namespacePrefix; 
}
0

Not sure if this will help or not, but the same error occurs in my dev org.

It appears to be related to the Execute Anonymous window.

I've created the following class:

public class testCode
{
    public static void testie()
    {
        String qualifiedUrl = new PageReference('/apex/').getUrl();
        system.debug(qualifiedUrl);
    }
}

If I run either:

testCode.testie();

or

String qualifiedUrl = new PageReference('/apex/').getUrl();
system.debug(qualifiedUrl);

from execute anonymous, I get that error.

If however I create a test class, it works fine:

@IsTest
public class testtest {

    public static testMethod void test()
    {
        testCode.testie();
    }
}

You must log in to answer this question.

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