@wire decorator for reactive data or call methods imperatively for user-initiated actions.
Apex Controller Setup
Create Apex methods with the@AuraEnabled annotation. Use cacheable=true for read-only methods that can be cached.
Apex Method Requirements
- Methods must be static
- Annotate with @AuraEnabled
- Use cacheable=true for read-only methods
- Use WITH USER_MODE to enforce sharing rules and field-level security
- Classes should use with sharing to enforce record-level security
Wire Service Approach
Use@wire for methods that load data automatically when a component initializes or when reactive parameters change.
Wire to Property
The simplest approach for displaying data without transformation.- Simple data display
- No data transformation needed
- Minimal error handling
Wire to Function
Provides more control for processing results and handling errors.- Data transformation required
- Custom error handling
- Setting multiple properties
- Conditional logic based on results
Imperative Approach
Call Apex methods imperatively for user-triggered actions like button clicks.- User-initiated actions (button clicks, form submissions)
- Multiple sequential Apex calls
- Conditional Apex calls
- Need to call methods dynamically
Parameters
Pass parameters to Apex methods using an object:Comparison: Wire vs Imperative
- Wire Service
- Imperative
- Automatic caching
- Reactive to parameter changes
- No manual lifecycle management
- Shared cache across components
- Loading data on component initialization
- Reactive data that updates with parameters
- Read-only operations
Error Handling
Best Practices
Use Cacheable
Set
cacheable=true on read-only Apex methods for better performanceEnforce Security
Always use
WITH USER_MODE and with sharing to respect user permissionsChoose the Right Approach
Use @wire for automatic loading, imperative for user actions
Handle Errors
Always implement error handling for better user experience
