PDI · Question #9
A Lightning component has a wired property, searchResults, that stores a list of Opportunities. Which definition of the Apex method, to which the searchResults property is wired, should be used?
The correct answer is A. @AuraEnabled(cacheable=true). The question asks for the correct definition of an Apex method wired to a Lightning component property, specifically for searchResults storing a list of Opportunities.
Question
A Lightning component has a wired property, searchResults, that stores a list of Opportunities. Which definition of the Apex method, to which the searchResults property is wired, should be used?
Options
- A@AuraEnabled(cacheable=true)
- B@AuraEnabled(cacheable=true) public List<Opportunity> search(String term)
- C@AuraEnabled(cacheable=false) public static List<Opportunity> search(String term)
- D@AuraEnabled(cacheable=false) public List<Opportunity> search(String term)
How the community answered
(25 responses)- A88% (22)
- B8% (2)
- D4% (1)
Why each option
The question asks for the correct definition of an Apex method wired to a Lightning component property, specifically for `searchResults` storing a list of Opportunities.
For an Apex method to be wired to a Lightning Web Component property, it must be decorated with the `@AuraEnabled(cacheable=true)` annotation, enabling client-side caching and proper functioning with the wire service. (The complete definition also requires `public static` and the correct return type and parameters).
This definition is missing the `static` keyword, which is a mandatory requirement for Apex methods that are invoked from Lightning Web Components via the wire service.
While the method includes the `static` keyword, the `cacheable=false` attribute is not recommended for methods used by wired properties as it prevents client-side caching and can negatively impact component performance.
This definition is incorrect as it lacks the `static` keyword, which is mandatory, and specifies `cacheable=false`, which prevents the method from being effectively used by a wired property for cached data retrieval.
Concept tested: Apex @AuraEnabled(cacheable=true) for LWC wired properties
Source: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex_wire_method
Topics
Community Discussion
No community discussion yet for this question.