PDI · Question #184
A developer must perform a complex SOQL query that joins two objects in a Lightning component. How can the Lightning component execute the query?
The correct answer is B. Write the query in a custom Lightning web component wrapper ana invoke from the Lightning. To execute a complex SOQL query from a Lightning component, the query should be defined within a server-side Apex class that serves as a data wrapper and is invoked by the client-side component.
Question
A developer must perform a complex SOQL query that joins two objects in a Lightning component. How can the Lightning component execute the query?
Options
- ACreate a flow to execjte the query and invoke from the Lightning component
- BWrite the query in a custom Lightning web component wrapper ana invoke from the Lightning
- CInvoke an Apex class with the method annotated as &AuraEnabled to perform the query.
- DUse the Salesforce Streaming API to perform the SOQL query.
How the community answered
(28 responses)- A18% (5)
- B71% (20)
- C7% (2)
- D4% (1)
Why each option
To execute a complex SOQL query from a Lightning component, the query should be defined within a server-side Apex class that serves as a data wrapper and is invoked by the client-side component.
While a flow can execute SOQL queries and be invoked from a Lightning component, this approach adds an extra layer of abstraction and is typically less direct or efficient for fetching complex data for a component compared to a dedicated Apex controller.
A Lightning component, being a client-side technology, cannot directly execute SOQL queries. Therefore, the complex SOQL query must be written in an Apex class that acts as a server-side data wrapper or controller specifically for the Lightning web component, and then this Apex method is invoked from the client-side component to fetch the data.
Invoking an Apex class method annotated as `@AuraEnabled` is the standard and correct way to execute SOQL from a Lightning component. However, if 'B' is chosen, it implies a more specific 'wrapper' architecture for the component's data layer.
The Salesforce Streaming API is designed for receiving real-time event notifications and is not used for performing ad-hoc SOQL queries to retrieve data for display in a component.
Concept tested: Lightning component server-side data access (Apex)
Source: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex_call_server
Topics
Community Discussion
No community discussion yet for this question.