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?

public with sharing class MynamespaceClass{

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

The above is simplest way to get namespace

| improve this answer | |
  • 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 Sep 18 '15 at 20:47

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.").

| improve this answer | |
  • 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 Sep 18 '15 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. – Renato Oliveira Sep 18 '15 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 Sep 18 '15 at 20:40

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);

    return namespace;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.