3

I am trying to create two second-generation (2GP) packages with the same namespace but running into an issue where one of the packages is not able to access the properties of a class from the other package despite the class being annotated with @namespaceAccessible and throwing an error.

@namespaceAccessible
public with sharing class AuraResult {

    public Boolean success = false;
    public String message = ''; 

    @namespaceAccessible
    public AuraResult() {

    }

    @namespaceAccessible
    public Boolean getSuccess() {
        return success;
    }

    @namespaceAccessible
    public void setSuccess(Boolean success) {
        this.success = success;
    }

    @namespaceAccessible
    public String getMessage() {
        return message;
    }

    @namespaceAccessible
    public void setMessage(String message) {
        this.message = message;
    }
}

In the example above, I am able to access the @namespaceAccessible methods i.e., getMessage() and setMessage() methods, but unable to access the properties directly:

AuraResult ar = new AuraResult();
String message = ar.getMessage(); // Works
Boolean success = ar.getSuccess(); // Works

message = ar.message; // Throws an error: "Variable is not visible".
success = ar.success; // Throws an error: "Variable is not visible".

Using the getter and setter methods is not ideal as it requires a lot of refactoring in my package. Is this expected behavior or a salesforce bug?

  • Could you please edit to include a minimal reproduction of the issue in code? – David Reed Feb 29 at 3:25
  • @david Updated the question with sample code. – Vinod Rondla Feb 29 at 3:41
2

Edit: This is a Known Issue. Make sure you mark "This affects me" if you want to be updated when this updated.


You need to annotate the members if you want them to be directly accessible:

@namespaceAccessible public Boolean success = false;
@namespaceAccessible public String message = ''; 

The docs specifically state:

The @namespaceAccessible makes public Apex in a package available to other packages that use the same namespace. Without this annotation, Apex classes, methods, interfaces, and properties defined in a 2GP package aren’t accessible to the other packages with which they share a namespace. Apex that is declared global is always available across all namespaces, and needs no annotation. (emphasis added).

You need to explicitly grant access per member.

| improve this answer | |
  • @sfdxfox I should've added this to the question but I was getting "NamespaceAccessible is not allowed on properties" error when I tried to make this change in my package. What I've just realized is that some of my class variables are declared with {get;set;} and these seem to be the ones throwing the error but not the variables without {get;set;}. Not sure if this is the expected behavior. – Vinod Rondla Mar 1 at 17:46
  • @VinodRondla The error doesn't seem to be in line with the documentation. I'll throw a message over at the documentation team, but for now, you'll need to use variables and not properties. You only to use {get;set;} anyways if you're writing a Visualforce controller, and you can always use getX and setX methods as a workaround if you need that capability. – sfdcfox Mar 1 at 19:19
  • @VinodRondla This is a Known Issue (added to answer). For now, you'll need to not use properties (i.e. use get/set methods, or find other alternatives). – sfdcfox Mar 5 at 17:31
  • @sfdxfox Accepted this as answer and marked "This affects me" on the issue. Thanks for your help. – Vinod Rondla Mar 5 at 22:01

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.