3

In an app that I'm creating, there are some places where I need to create objects based on their string name, and need to take the apps namespace prefix into account dynamically.

I've created this function that will grab the ns based on one of the custom object names:

public static String getNamespacePrefix() {
    String namespace = '';
    try {
        ApexClass ac = [SELECT NameSpacePrefix FROM ApexClass WHERE Name = 'CUSTOM_CLASS_NAME'];
        if(null != ac.NameSpacePrefix) {
            namespace = ac.NameSpacePrefix + '__';
        }
    } catch (QueryException ex) {
        // do nothing
    }
    return namespace;
}

There are a handful of places that call this function which is causing me to run into SQL query limits.

Is there a better way to do this? Or a way to make the namespace somehow globally available one time?

3 Answers 3

2
public with sharing class MynamespaceClass{

   public static final String CURRENTNAMESPACE = MynamespaceClass.class.getName().substringBefore('MynamespaceClass').removeEnd('.');
 }

The above is simplest way to get namespace

1
  • This has the same issue as the one Renato gave. It works with unpackaged code, but when trying it on packaged unmanaged or managed code I get the error Variable does not exist: ObjectName__.class
    – Dan Bough
    Commented Sep 18, 2015 at 20:47
1

Yes, you can get it from a custom object (that has the namespace prefix, of course).

public static String getNamespace_Underline()
{
    DescribeSObjectResult describe = YourCustomObject__c.SObjectType.getDescribe();

    String name = describe.getName();
    String localName = describe.getLocalName();

    return name.removeEnd(localName);
}

This would return your prefix with a dot at the end ("myprefix.").

3
  • 1
    This doesn't seem to work with installed packages (managed or unmanaged). I get an error: Variable does not exist: ObjectName__c.SObjectType. It seems to work fine on unpackaged custom objects though.
    – Dan Bough
    Commented Sep 18, 2015 at 19:43
  • Are you running this in the developer console? If so, you'll need to add the prefix into the object's variable. My example works for me because when it compiles, Salesforce automatically appends the namespace to the variable, I think. Same thing happens when the package is ran in the tests and in the target org. Commented Sep 18, 2015 at 19:49
  • Yep, I was using the console. I think the solution is good, but since I can't debug in the console I had to find another solution.
    – Dan Bough
    Commented Sep 18, 2015 at 20:40
0

I found a solution on this page and ended up going with the following:

public static String getNamespacePrefix() {
    Integer indexOfToken = -1;
    String namespace = '';
    for ( Schema.SObjectType type : Schema.getGlobalDescribe().values() ) {
        String sobjName = String.valueOf(type);

        if (null != sobjName) {
            indexOfToken = sobjName.indexOf('ObjectName__c');
        }

        if (indexOfToken != -1) {
            namespace = sobjName.substring(0, indexOfToken);
            break;
        }
    }

    return namespace;
}

You must log in to answer this question.

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