Tuesday, May 8, 2012

Salesforce - Referencing an installed Managed Packages code from Apex

When I first tried to reference an Apex class within an installed managed package I ended up with:

Error: Compile Error: Type is not visible: apexclassinmanagedpackage at line 7 column 36

To make this work I needed to add a specific reference from the Apex class to a version of the managed package in the version settings tab.

From the help documents:

Click Version Settings to specify the version of Apex and the API used with this class. If your organization has installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this class. Use the default values for all versions. This associates the class with the most recent version of Apex and the API, as well as each managed package. You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version. You can specify an older version of Apex and the API to maintain specific behavior.

This could also be handled in the <Your Class>.cls-meta.xml file locally.


The class, any applicable constructors, and method being called must also be marked as global rather than public. From the help documents:

  • The public access modifier declares that this class is visible in your application or namespace.
  • The global access modifier declares that this class is known by all Apex code everywhere. All classes that contain methods defined with the webService keyword must be declared as global. If a method or inner class is declared as global, the outer, top-level class must also be defined as global.

Also from the help documents:

global
This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the SOAP API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global.
Note
We recommend using the global access modifier rarely, if at all. Cross-application dependencies are difficult to maintain.

See Also: