function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dJadhavdJadhav 

How to get namespace prefix of the organization?

Hi,

 

Is there a way to get the namespace prefix of the organization in apex?  

 

Thanks in advance.

 

Regards,

Dipak

Best Answer chosen by Admin (Salesforce Developers) 
dJadhavdJadhav

Hi All,

 

I have solved the issue.

 

Please see the following solution:

  

 ApexClass cs =[select NamespacePrefix from ApexClass where Name =:'<name of the class from your package>'];
                nameSpacePrefix= cs.NamespacePrefix;

Regards,

Dipak

All Answers

dJadhavdJadhav

Hi All,

 

I have solved the issue.

 

Please see the following solution:

  

 ApexClass cs =[select NamespacePrefix from ApexClass where Name =:'<name of the class from your package>'];
                nameSpacePrefix= cs.NamespacePrefix;

Regards,

Dipak

This was selected as the best answer
Mitesh SuraMitesh Sura
This will work for managed package because "MyClass' will already have a namespace (our namespace). So that query will always return our namespace and not the org namespace where the app is installed.
Daniel BallingerDaniel Ballinger
Since the Spring '16 release it has been possible to query Organization.NamespacePrefix. See How can I get organization's namespace prefix (http://salesforce.stackexchange.com/a/103034/102).
Mitesh SuraMitesh Sura
Typo in my previous comment. What I meant was it will NOT work in managed package. 

@Daniel, I have upvoted your answer. 
Gabriel WondersGabriel Wonders
Does the Organization NamespacePrefix field solve the issue of the above solution not working in managed packages? For example, if I had the following code in a managed package that is installed on an org other than the package org, will this SOQL return null since I am outside my package org or is it smart enought (context aware) to figure out that the code is running in the context of my managed packge and get the correct namespace prefix?

string np = [SELECT NamespacePrefix FROM Organization].NamespacePrefix;
Abdulla d 5Abdulla d 5
HI, this can be achieved by using the tooling api
in query editor run this query
[select id,DeveloperName,NamespacePrefix from CustomObject];

yoou should check box tooling api, beside the execute button in developer console