Newer Version Available

This content describes an older version of this product. View Latest

Access Controllers From Visualforce Pages in Different Packages

To access an Apex controller from a Visualforce page that is in a different package, use the @namespaceAccessible Apex annotation in your custom controller class. In first-generation packaging, you can develop only one managed package with a given namespace. In second-generation packaging, you can develop more than one managed (or unlocked) package with the same namespace. By default, Visualforce pages installed in a package can't call a public Apex method from an Apex class in another package. This is true even if both packages are in the same namespace.

Here’s an example of how to include the @namespaceAccessible annotation in your Apex code.

1@namespaceAccessible
2public virtual class NsController {
3    private String message;
4    @namespaceAccessible
5    public NsController() {
6        this.message = 'default'; // init to non-blank value
7    }
8    @namespaceAccessible
9    public virtual String getMessage() {
10       return this.message;
11    }
12    @namespaceAccessible
13    public virtual void setMessage(String msg) {
14        this.message = msg;
15    }
16}

First, add the annotation above the controller, which makes it visible from across the namespace in different packages. You must annotate the controller so that any methods you add to the annotation are also visible. Then, for every method that you want visible across the namespace, add the @namespaceAccessible annotation before the method. Use the annotation only for methods that you want visible outside of your package, but within the same namespace.