Refer to the following code snippet:
Java
public class LeadController {
public static List getFetchLeadList(String searchTerm, Decimal aRevenue) {
String safeTerm = '%'+searchTerm.escapeSingleQuotes()+ '%';
return [
SELECT Name, Company, AnnualRevenue
FROM Lead
WHERE AnnualRevenue >= :aRevenue
AND Company LIKE :safeTerm
LIMIT 20
];
}
}
A developer created a JavaScript function as part of a Lightning web component (LWC) that surfaces information about Leads by wire calling getFetchLeadList when certain criteria are met. Which three changes should the developer implement in the Apex class above to ensure the LWC can display data efficiently while preserving security?1
Suggested Answer: A, B, C
Explanation:
Compreh17ensive and Detailed 11850 to 250 words of
To make an Apex method compatible with a Lightning Web Component's @wire service and ensure it follows security best practices, three specific modifications are required:
@AuraEnabled(Cacheable=true) (Option C): The @wire service in LWC requires the Apex method to be marked as cacheable. This enables client-side caching via the Lightning Data Service, which significantly improves UI performance by reducing redundant server calls. Note that Cacheable=true is mandatory for @wire but optional for imperative calls.
with sharing (Option B): In Apex, classes do not enforce sharing rules by default. To ensure the user only sees Leads they have access to according to the organization-wide defaults and sharing model, the class must explicitly use the with sharing keyword.
WITH SECURITY_ENFORCED (Option A): While with sharing handles record-level access, it does not automatically enforce field-level security (FLS) or object-level security (CRUD). Adding the WITH SECURITY_ENFORCED clause to the SOQL query ensures that if a user does not have permission to view the AnnualRevenue field, the query will throw an exception rather than exposing protected data.
Options D and E are incorrect because without sharing bypasses security, and a simple @AuraEnabled without cacheable=true is insufficient for the LWC @wire service.