The correct syntax for using @wire to connect a Lightning Web Component property to an Apex method requires specifying the method and a configuration object that includes the reactive property prefixed with $. The correct usage is:
javascript
CopyEdit
@wire(getAccounts, { searchTerm: '$searchTerm' })
This correctly wires the reactive searchTerm property to the getAccounts method.
[Reference:, Wire a Property to an Apex Method, , To determine the correct definition of a Lightning Web Component (LWC) property that uses the getAccounts Apex method, we need to evaluate the syntax and usage of the @wire decorator in LWC, focusing on how it connects to Apex methods and passes parameters. Let’s analyze the problem and each option systematically, referencing Salesforce’s official Lightning Web Components Developer Guide., Understanding the Requirement:, Apex Method: The getAccounts method is an Apex method that returns a List and takes a parameter searchTerm. For an Apex method to be callable from an LWC, it must be annotated with @AuraEnabled(cacheable=true) (for @wire) and be static. The Lightning Web Components Developer Guide states: “To call an Apex method from a Lightning Web Component using @wire, the method must be static and annotated with @AuraEnabled(cacheable=true)” (Salesforce Lightning Web Components Developer Guide, Call Apex Methods)., LWC Property: The question asks for the correct definition of an LWC property that uses @wire to call getAccounts. The @wire decorator is used to wire a property or function to a data source, such as an Apex method, and can pass dynamic parameters., Parameter Passing: The searchTerm parameter must be passed dynamically to getAccounts, meaning its value comes from a reactive property (e.g., searchTerm) in the LWC. In LWC, reactive properties are tracked for changes, and the $ prefix is used to indicate reactivity in @wire parameters., LWC @wire Syntax:, The @wire decorator connects a property or function to a data source (e.g., an Apex method). When wiring to an Apex method, the syntax is: , javascript, Copy, @wire(apexMethod, { param1: '$property1', param2: '$property2' }), propertyName;, Apex Method Reference: The apexMethod is the imported Apex method (e.g., getAccounts imported from a controller)., Parameters: The second argument is an object mapping Apex method parameters to LWC properties. The $ prefix makes the property reactive, meaning the wired method re-invokes when the property changes. The Lightning Web Components Developer Guide explains: “Use the $ prefix in the parameters object to indicate a reactive property, so the wired method is called when the property’s value changes” (Salesforce Lightning Web Components Developer Guide, Pass Parameters to Apex Methods)., Result: The wired property receives an object with data (the Apex method’s return value) or error (if an error occurs)., Evaluating the Options:, A. @wire(getAccounts, { searchTerm: '$searchTerm' }) , Syntax: Uses @wire to call getAccounts and passes parameters as an object { searchTerm: '$searchTerm' }., Parameter Mapping: The searchTerm parameter of the getAccounts Apex method is mapped to the LWC’s searchTerm property. The $searchTerm syntax indicates that searchTerm is a reactive property, and getAccounts will be re-invoked if searchTerm changes., Correctness: This matches the standard LWC syntax for wiring an Apex method with parameters. The Lightning Web Components Developer Guide confirms: “Pass parameters to an Apex method as a JavaScript object, using the $ prefix for reactive properties” (Salesforce Lightning Web Components Developer Guide, Call Apex Methods)., Conclusion: Correct, as it uses the proper @wire syntax and parameter format., B. @track(getAccounts, '$searchTerm') , Syntax: Uses @track instead of @wire., Decorator: The @track decorator is used to make a property reactive, meaning the component re-renders when the property changes, but it does not wire the property to a data source like an Apex method. The Lightning Web Components Developer Guide states: “@track is used to mark a property as reactive for re-rendering, but it does not fetch data from a server” (Salesforce Lightning Web Components Developer Guide, Reactive Properties)., Parameter: Passing getAccounts and '$searchTerm' to @track is invalid, as @track does not accept arguments in this manner., Conclusion: Incorrect, as @track cannot be used to wire an Apex method., C. @wire(getAccounts, 'searchTerm: $searchTerm') , Syntax: Uses @wire to call getAccounts, but the parameters are passed as a string 'searchTerm: $searchTerm'., Parameter Format: The @wire decorator expects the second argument to be a JavaScript object (e.g., { searchTerm: '$searchTerm' }), not a string. The Lightning Web Components Developer Guide specifies: “The second argument to @wire for an Apex method must be an object mapping parameter names to values” (Salesforce Lightning Web Components Developer Guide, Pass Parameters to Apex Methods). Passing a string like 'searchTerm: $searchTerm' results in a runtime error or the Apex method not being called correctly., Conclusion: Incorrect, as the parameter format is invalid (string instead of an object)., D. @wire(getAccounts, '$searchTerm') , Syntax: Uses @wire to call getAccounts, but passes '$searchTerm' directly as a string, not as a parameter object., Parameter Format: The getAccounts method expects a parameter named searchTerm, so the correct format is { searchTerm: '$searchTerm' }. Passing '$searchTerm' as a single value does not map to the Apex method’s parameter name, causing the method to receive no value for searchTerm (or fail entirely). The Lightning Web Components Developer Guide notes: “Parameter names in the object must match the Apex method’s parameter names” (Salesforce Lightning Web Components Developer Guide, Call Apex Methods)., Conclusion: Incorrect, as the parameter is not passed as a properly formatted object mapping to the Apex method’s parameter., Why Option A is Correct:, Option A is correct because:, It uses the @wire decorator to properly connect the getAccounts Apex method to an LWC property., It passes the searchTerm parameter in the correct format: { searchTerm: '$searchTerm' }, mapping the Apex method’s parameter to the LWC’s reactive searchTerm property., The $searchTerm syntax ensures reactivity, so the getAccounts method is re-invoked when searchTerm changes, aligning with LWC best practices., This matches the standard syntax outlined in the Salesforce Lightning Web Components Developer Guide for wiring Apex methods with parameters., Example for Clarity:, Here’s how option A would be used in a complete LWC JavaScript file:, javascript, Copy, import { LightningElement, wire } from 'lwc';, import getAccounts from '@salesforce/apex/AccountController.getAccounts';, , export default class MyComponent extends LightningElement {, searchTerm = ''; // Reactive property for search term, , // Wire the getAccounts Apex method to a property, @wire(getAccounts, { searchTerm: '$searchTerm' }), accounts;, , // Example: Update searchTerm based on user input, handleSearchTermChange(event) {, this.searchTerm = event.target.value;, }, }, Apex Controller (for reference):, apex, Copy, public with sharing class AccountController {, @AuraEnabled(cacheable=true), public static List getAccounts(String searchTerm) {, return [SELECT Id, Name FROM Account WHERE Name LIKE :('%' + searchTerm + '%')];, }, }, Behavior: When searchTerm changes (e.g., due to user input), the @wire decorator re-invokes getAccounts with the new searchTerm value, and the accounts property receives the result (e.g., { data: [/* Account records */], error: undefined })., Handling Typos:, The options are syntactically correct in the provided image, with no typos to address. However, the options assume getAccounts is properly imported and defined in the Apex controller, which we infer based on the question’s context., The question’s phrasing is clear, and the options align with typical LWC syntax patterns., References:, Salesforce Lightning Web Components Developer Guide: , “Call Apex Methods” section: Details the use of @wire to call Apex methods, including parameter passing., “Pass Parameters to Apex Methods” section: Explains the { param: '$property' } syntax for reactive parameters., “Reactive Properties” section: Clarifies the role of @track (and why it’s not applicable here).(Available at: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/), Salesforce Apex Developer Guide: , “AuraEnabled Annotation” section: Describes requirements for Apex methods to be callable from LWC (@AuraEnabled(cacheable=true)).(Available at: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/), Platform Developer I Study Guide: , Section on “User Interface”: Covers building LWCs, including wiring Apex methods and handling reactivity.(Available at: https://trailhead.salesforce.com/en/content/learn/modules/platform-developer-i-certification-study-guide), , ]
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit