Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a dev org, in which I have got a namespace prefix. In apex, I want to get this namespace prefix as string. Is there any straight way or I have to describe some object?

share|improve this question
Unless I am missing something , 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. – Salesforce Developer Dec 20 '14 at 14:05
up vote 8 down vote accepted

Query for it, using a class that you know will be there:

ApexClass myClass = [
    select NamespacePrefix
    from ApexClass
    where Name = 'SomeClassNameHere'
share|improve this answer
Does this require any permissions that ordinary users may not have? – kresho May 4 at 12:45
Hm good question. I think it should just work for all users when queried from Apex, but you'd want to test that to be sure. – jkraybill May 7 at 5:10

Spotted in the Spring `16 release notes under Changed Objects:


The following new fields have been added:

  • NamespacePrefix
  • SignupCountryIsoCode—Represents the two-character ISO country code specified by the user for a sign-up request.

It appears it will now be possible to directly query for the NamespacePrefix without needing to have a known Apex Class or Trigger present.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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