PDI · Question #121
Cloud Kicks Fitness, an ISV Salesforce partner, is developing a managed package application. One of the application modules allows the user to calculate body fat using the Apex class, BodyFat, and…
The correct answer is D. Declare the class and method using the global access modifier. For Apex components within a managed package to be accessible to subscribers outside the package's namespace, both the class and its methods must be declared with the global access modifier.
Question
Cloud Kicks Fitness, an ISV Salesforce partner, is developing a managed package application. One of the application modules allows the user to calculate body fat using the Apex class, BodyFat, and its method, calculateBodyFat(). The product owner wants to ensure this method is accessible by the consumer of the application when developing customizations outside the ISV's package namespace. Which approach should a developer take to ensure calculateBodyFat() is accessible outside the package namespace?
Options
- ADeclare the class and method using the public access modifier.
- BDeclare the class as global and use the public access modifier on the method.
- CDeclare the class as public and use the global access modifier on the method.
- DDeclare the class and method using the global access modifier.
How the community answered
(55 responses)- A5% (3)
- B5% (3)
- C2% (1)
- D87% (48)
Why each option
For Apex components within a managed package to be accessible to subscribers outside the package's namespace, both the class and its methods must be declared with the `global` access modifier.
The `public` access modifier restricts access to components within the same application or namespace and does not allow external access from outside a managed package.
While the class is `global`, a `public` method within a `global` class will not be accessible outside the package's namespace; the method itself must also be `global`.
If the class is `public`, it cannot be accessed from outside the package's namespace, rendering a `global` method within it inaccessible to subscribers.
To allow external access from outside the package's namespace, both the Apex class (`BodyFat`) and its method (`calculateBodyFat()`) must be declared using the `global` access modifier.
Concept tested: Apex access modifiers for managed packages
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm
Topics
Community Discussion
No community discussion yet for this question.