Is there a way to get the Namespace Prefix of Managed Package where I just have the Id of? Maybe it is helpful that my code sits in the same org as the LMA app in with its sfLma__Package__c objects.

  • Something like SELECT NamespacePrefix FROM PackageLicense WHERE ...?
  • Or something using Tooling, Metadata or SOAP API?

Example code would be helpful.

  • For first generation packages, you may be stuck with adding and populating your own custom field on the sfLma__Package__c object. – Keith C May 29 '19 at 10:06
  • No way to populate such a custom field via Code?! – Robert Sösemann May 29 '19 at 10:27

You can use the Tooling API to query SubscriberPackage, e.g.

$ force query -t "SELECT NamespacePrefix FROM SubscriberPackage WHERE Id = '033f40000001ZCXAA2'"
 (1 records)

Or if you have the Id of the version of the package, you can query InstalledSubscriberPackage, e.g.

$ force query -t "SELECT SubscriberPackage.NamespacePrefix FROM InstalledSubscriberPackage WHERE Id = '0A3540000004uhzCAA'"
 (1 records)
| improve this answer | |
  • But can I do this in Apex? – Robert Sösemann May 29 '19 at 14:42
  • I don't know of a way to do it from Apex (without a callout to the API). – xn. May 29 '19 at 14:49
  • A callout would be ok – Robert Sösemann May 29 '19 at 14:54
  • Maybe use @afawcett's library: github.com/afawcett/apex-toolingapi – xn. May 29 '19 at 14:57
  • 2
    You can query SubscriberPackage by Id in any org; package ids are globally unique. You can only query InstalledSubscriberPackage for packages installed in the org. – xn. May 29 '19 at 19:17

Doing via Apex and the RESTAPI is pretty simple. The setup like getting the tooling endpoint and session Id varies depending on your exact implementation. The below assumes you know how to get them and uses a token to represent them:

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + {$SESSIOIN_ID});

String toolingURL = '{!$TOOLING_ENDPOINT}/query/?q=SELECT+SubscriberPackage.NamespacePrefix+From+InstalledSubscriberPackage+WHERE+ID+=\'{$PACKAGEID}\'';


Then you send it. The response is the typical JSON that would deserialize as below. You will have to adjust to add the cross object field that is returned and add it to the records class below

public class restQueryResults {
    public Integer totalSize;
    public records[] records { get; set; }

public class records {
    public String id { get; set; }
    public String name;
    public String description;
    public Boolean success;

Is that what you need to get you started?

| improve this answer | |
  • 2
    Just to add to this: since Winter'19 to define the Tooling endpoint in apex you can use System.Url.getOrgDomainUrl(). – Jeferson Chaves May 29 '19 at 15:44

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.