Determine the Apex class's namespace

Salesforce provides a standard object called ApexClass, by using this object you can get the Namespace.

Query on ApexClass:

ApexClass ac = [SELECT NameSpacePrefix FROM ApexClass WHERE Name = 'YourClassName'];


The UserInfo.isCurrentUserLicensed(String) method can be used for this without a SOQL query.

If the namespace passed in isn't valid in the current org (due to the package not being installed) a TypeException is thrown, which would indicate you're working with an unmanaged version of your codebase.

If the method returns true you're in a managed environment, and false means you're in a managed environment but aren't licensed for the package (which wouldn't happen in the packaging org).

Tags:

Apex