@AuraEnabled Methods and Wire Adapters
@AuraEnabled Methods and Wire Adapters are two fundamental mechanisms in Salesforce for communication between Lightning Web Components (LWC) and the server (Apex). **@AuraEnabled Methods:** These are Apex methods decorated with the @AuraEnabled annotation, making them accessible from Lightning com… @AuraEnabled Methods and Wire Adapters are two fundamental mechanisms in Salesforce for communication between Lightning Web Components (LWC) and the server (Apex). **@AuraEnabled Methods:** These are Apex methods decorated with the @AuraEnabled annotation, making them accessible from Lightning components. Key characteristics include: 1. **@AuraEnabled(cacheable=true):** Marks the method as read-only and cacheable, improving performance by storing results in the client-side cache. Cacheable methods cannot perform DML operations. 2. **@AuraEnabled without cacheable:** Used for methods that perform DML operations (insert, update, delete). These are called imperatively from JavaScript. 3. Methods must be **static** and **public** or **global** in scope. **Wire Adapters:** The wire service is a reactive mechanism in LWC that provisions data to components. It uses the @wire decorator to bind a property or function to a wire adapter. 1. **Wire to Property:** Data is automatically provisioned to a component property. The property receives an object with `data` and `error` attributes. 2. **Wire to Function:** Data is provisioned through a function, offering more control over data handling and processing. 3. **Built-in Wire Adapters:** Salesforce provides adapters like `getRecord`, `getObjectInfo`, `getPicklistValues`, and `getListUi` from the `lightning/ui*Api` modules, eliminating the need for custom Apex. 4. **Custom Wire Adapters:** Using @AuraEnabled(cacheable=true) Apex methods with the wire service via `import methodName from '@salesforce/apex/ClassName.methodName'`. **Key Differences:** - Wire adapters are **reactive** — they automatically re-fetch data when parameters change. - Imperative Apex calls provide more **control** over when the call executes. - Wire adapters require **cacheable=true**, while imperative calls can perform DML. - Wire service calls are **optimized** by the framework with built-in caching and deduplication. For the Platform Developer I exam, understanding when to use wire versus imperative calls, caching behavior, and the proper annotations is essential for building efficient Lightning components.
@AuraEnabled Methods and Wire Adapters – Complete Guide for Salesforce Platform Developer 1
Why @AuraEnabled Methods and Wire Adapters Matter
In the Salesforce Platform Developer 1 exam, understanding how Lightning Web Components (LWC) and Aura Components communicate with Apex is absolutely critical. The two primary mechanisms for this communication are @AuraEnabled methods and wire adapters. These topics appear frequently across multiple question types, and a solid understanding of them is essential for passing the exam.
Modern Salesforce development relies heavily on the Lightning Component framework, and nearly every real-world application needs to read from or write to the server. @AuraEnabled methods and wire adapters are the bridge between client-side components and server-side Apex logic.
What Are @AuraEnabled Methods?
An @AuraEnabled method is an Apex method that has been annotated with the @AuraEnabled annotation. This annotation exposes the method so it can be called from Lightning components (both Aura and LWC).
Key characteristics:
- The method must be static and public (or global).
- It must be defined in an Apex class (the class itself does not need any special annotation for LWC, but for Aura it must be accessible).
- Parameters and return types must be serializable (primitive types, sObjects, collections, or custom Apex types with @AuraEnabled properties).
- You can use @AuraEnabled(cacheable=true) to mark a method as cacheable, meaning the Lightning Data Service can cache its results on the client side.
Example of an @AuraEnabled method:
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
return [SELECT Id, Name, Industry FROM Account LIMIT 10];
}
@AuraEnabled
public static Account createAccount(String name) {
Account acc = new Account(Name = name);
insert acc;
return acc;
}
}
What Are Wire Adapters?
Wire adapters are a reactive mechanism provided by the Lightning Web Components framework that allows components to declaratively fetch data. They are part of the Lightning Data Service (LDS) and leverage the @wire decorator.
Two categories of wire adapters:
1. Built-in (LDS) wire adapters – Provided by Salesforce out of the box, such as:
- getRecord – Fetches a single record
- getRecordUi – Fetches record data with layout metadata
- getListUi – Fetches list view data
- getObjectInfo – Fetches object metadata
- getPicklistValues – Fetches picklist values
- getPicklistValuesByRecordType – Fetches picklist values by record type
2. Custom wire adapters (Apex wire methods) – You can wire an @AuraEnabled(cacheable=true) Apex method to a property or function in LWC.
How the @wire Decorator Works
The @wire decorator in LWC tells the framework to provision data reactively. When the parameters to a wired property or function change, the framework automatically re-invokes the wire adapter and provides updated data.
Wiring to a property:
import { LightningElement, wire } from 'lwc';
import getAccounts from '@salesforce/apex/AccountController.getAccounts';
export default class AccountList extends LightningElement {
@wire(getAccounts)
accounts;
}
When wired to a property, the property receives an object with two fields: data and error. You access results via this.accounts.data and errors via this.accounts.error.
Wiring to a function:
import { LightningElement, wire } from 'lwc';
import getAccounts from '@salesforce/apex/AccountController.getAccounts';
export default class AccountList extends LightningElement {
accountList;
error;
@wire(getAccounts)
wiredAccounts({ error, data }) {
if (data) {
this.accountList = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.accountList = undefined;
}
}
}
Wiring to a function gives you more control: you can process or transform data before assigning it.
Passing parameters to wired Apex methods:
@wire(getContactsByAccountId, { accountId: '$selectedAccountId' })
contacts;
The $ prefix makes the parameter reactive. When selectedAccountId changes, the wire service automatically re-fetches data.
Imperative Apex Calls vs. Wire Service
Not all server calls should use @wire. Understanding when to use each approach is a critical exam topic.
Use @wire when:
- You want to read data reactively (the data should refresh when parameters change).
- The method is cacheable (@AuraEnabled(cacheable=true)).
- You do NOT need to perform DML operations.
- You want the framework to manage the lifecycle of data provisioning.
Use imperative Apex calls when:
- You need to perform DML operations (insert, update, delete).
- You need to call the method in response to a user action (e.g., button click).
- You need more control over when the call is made.
- The method is NOT cacheable.
Example of an imperative call:
import createAccount from '@salesforce/apex/AccountController.createAccount';
handleCreate() {
createAccount({ name: this.accountName })
.then(result => {
// handle success
})
.catch(error => {
// handle error
});
}
Important: You CANNOT use @wire with a method that performs DML. Methods that perform DML must NOT have cacheable=true set, and @wire requires cacheable=true for Apex methods.
The cacheable=true Requirement
This is one of the most commonly tested distinctions:
- @AuraEnabled(cacheable=true): The method's result can be cached by the framework. Required for @wire in LWC. The method CANNOT perform DML operations.
- @AuraEnabled (without cacheable=true): The method can perform DML. It can only be called imperatively from LWC, not via @wire.
Wire Adapters from Lightning Data Service (LDS)
LDS wire adapters do NOT require custom Apex code. They use standard APIs to interact with data. Key benefits include:
- No Apex needed for basic CRUD operations
- Shared cache across components on the same page
- Automatic UI updates when data changes
- Respects CRUD/FLS automatically
Common LDS wire adapters to know for the exam:
- import { getRecord } from 'lightning/uiRecordApi';
- import { getObjectInfo } from 'lightning/uiObjectInfoApi';
- import { getPicklistValues } from 'lightning/uiObjectInfoApi';
Error Handling
When using @wire, errors are returned in the error property of the provisioned value. For imperative calls, errors are caught in the .catch() block of the Promise or in a try/catch with async/await.
A common error structure includes:
- error.body.message – The error message
- error.body.fieldErrors – Field-specific errors
- error.body.pageErrors – Page-level errors
Security Considerations
- Apex classes called from LWC respect the with sharing or without sharing keyword on the class.
- The exam expects you to know that with sharing enforces record-level security (sharing rules) but does NOT enforce CRUD/FLS automatically in Apex. You must manually check CRUD/FLS or use WITH SECURITY_ENFORCED in SOQL or Security.stripInaccessible().
- LDS wire adapters enforce CRUD/FLS automatically.
Refreshing Wired Data
After performing a DML operation imperatively, the wired data may become stale. To refresh it, use the refreshApex() function:
import { refreshApex } from '@salesforce/apex';
// Store the wired result reference
wiredAccountResult;
@wire(getAccounts)
wiredAccounts(result) {
this.wiredAccountResult = result;
// process data/error
}
// After DML
handleAfterSave() {
refreshApex(this.wiredAccountResult);
}
Important: refreshApex() requires the entire provisioned value object (not just the data part). This is why you store the full result when wiring to a function.
Aura Components and @AuraEnabled
While the exam is increasingly LWC-focused, you should also know how Aura components use @AuraEnabled methods:
- Aura uses $A.enqueueAction() to call server-side Apex methods.
- The Apex method must be @AuraEnabled.
- You set parameters with action.setParams().
- You handle the response in a callback with action.setCallback().
- Aura does NOT use @wire or the wire service.
Summary Table: Wire vs. Imperative
| Feature | @wire | Imperative |
|---|---|---|
| Requires cacheable=true | Yes (for Apex methods) | No |
| Can perform DML | No | Yes |
| Reactive to parameter changes | Yes | No (manual) |
| Data provisioned automatically | Yes | No (must call explicitly) |
| Called in response to user action | Not ideal | Yes |
| Error handling | error property | .catch() or try/catch |
Exam Tips: Answering Questions on @AuraEnabled Methods and Wire Adapters
1. Know the cacheable distinction cold: If a question involves @wire calling an Apex method, the method MUST have @AuraEnabled(cacheable=true). If it involves DML, it CANNOT be cacheable and CANNOT be wired.
2. DML = Imperative: Any scenario involving insert, update, delete, or upsert requires an imperative call, never @wire. This is one of the most commonly tested concepts.
3. Reactive parameters use the $ prefix: When you see @wire with parameters like { accountId: '$recordId' }, the $ makes it reactive. Without the $, it passes the literal string.
4. Wire to property vs. function: Wire to a property for simple data binding. Wire to a function when you need to process or transform data, or when you need to store the result reference for refreshApex().
5. refreshApex() requires the full provisioned object: A common trick question involves calling refreshApex() with just the data portion. You must pass the entire wired result object.
6. Static and public: @AuraEnabled methods must be static. If a question shows an instance method with @AuraEnabled, that is incorrect.
7. LDS vs. Custom Apex: If the scenario is a simple record read/write without complex logic, LDS (getRecord, updateRecord, createRecord) is the preferred approach. Custom Apex via @AuraEnabled is for complex business logic or queries.
8. Sharing and security: Remember that @AuraEnabled Apex classes should use with sharing to enforce record-level security. LDS automatically respects CRUD/FLS, but custom Apex does not unless you explicitly enforce it.
9. Return types matter: @AuraEnabled methods must return serializable types. Inner classes used as return types must have their properties annotated with @AuraEnabled as well (for Aura) or be public (for LWC).
10. Watch for trick answers mixing Aura and LWC syntax: Aura uses $A.enqueueAction(), while LWC uses @wire or imperative imports. Do not confuse the two.
11. Understand the lifecycle: @wire methods are called during component initialization and whenever reactive parameters change. Imperative calls happen only when explicitly invoked (e.g., in connectedCallback() or an event handler).
12. Read the question carefully for keywords: Words like 'automatically', 'reactively', 'when parameter changes' point to @wire. Words like 'on button click', 'save', 'create', 'delete' point to imperative calls.
13. Error handling patterns: Know that wired results provide { data, error } and that imperative calls use Promises (.then/.catch) or async/await with try/catch.
14. Eliminate wrong answers systematically: If an answer suggests using @wire for a DML operation, eliminate it immediately. If an answer shows an @AuraEnabled method without the static keyword, eliminate it.
15. Practice with scenarios: The exam loves scenario-based questions. Practice identifying whether a given use case calls for @wire with a built-in adapter, @wire with custom Apex, or an imperative Apex call. The correct choice always depends on whether the operation is read-only, whether it needs to be reactive, and whether DML is involved.
🎓 Unlock Premium Access
Salesforce Certified Platform Developer I + ALL Certifications
- 🎓 Access to ALL Certifications: Study for any certification on our platform with one subscription
- 2750 Superior-grade Salesforce Certified Platform Developer I practice questions
- Unlimited practice tests across all certifications
- Detailed explanations for every question
- PD1: 5 full exams plus all other certification exams
- 100% Satisfaction Guaranteed: Full refund if unsatisfied
- Risk-Free: 7-day free trial with all premium features!