Sub topic 2
Importing the Apex method into your component
This chapter assumes you know basic JavaScript (ES modules, objects, Promises), LWC fundamentals (templates, decorators, event handlers), and basic Apex. It also assumes you have an org or scratch org and can deploy to it. The Apex method you import here is the one exposed with @AuraEnabled in the previous chapter.
What the import does
An LWC cannot call Apex by name. Instead, you import the Apex method through a special module path. The platform resolves that path at build and deploy time and gives you a JavaScript function. Calling the function sends a request to the server and returns a Promise.
The path has a fixed shape:
@salesforce/apex/Namespace.ClassName.methodNameFor a class in your own org with no namespace, leave out the namespace:
@salesforce/apex/ContactController.searchContactsHere is the import in a component:
import { LightningElement } from 'lwc';
import searchContacts from '@salesforce/apex/ContactController.searchContacts';
export default class ContactSearch extends LightningElement {
searchTerm = '';
contacts = [];
}The import is a default import. The local name (searchContacts) is yours to choose, but matching the Apex method name keeps the code easy to read. If a managed package provides the class, include its namespace, for example @salesforce/apex/acme.ContactController.searchContacts.
How the pieces connect
The names must match across three places: the Apex class, the Apex method, and the import path. The following diagram shows the flow from the import to the server.
What the platform checks
The import is validated when you save or deploy the component. These rules apply:
- The Apex class must exist in the org and be accessible to the component's users.
- The method must be
staticand have the@AuraEnabledannotation. - The method must be
publicorglobal. - The class must be deployed before, or together with, the component.
If the path points to a missing class or method, deployment fails. You see an error like Invalid import: Apex method not found or a similar message. Check the spelling and capitalization of the class and method first.
Common errors
| Symptom | Likely cause |
|---|---|
| Deployment fails with an unresolved import | Misspelled class or method name in the path |
| Method not found at runtime | Method is not `@AuraEnabled`, or is not `static` |
| Access error when the user opens the component | The user's profile or permission set lacks access to the Apex class |
| Import works in one org but not another | The class is not deployed there, or a namespace is missing from the path |
The access error is the most common surprise. A user needs access to the Apex class through their profile or a permission set. Without it, the import resolves but the call fails when it runs.
Import only what you call
Each imported method becomes a separate function. If a component needs two methods, import both:
import searchContacts from '@salesforce/apex/ContactController.searchContacts';
import updateEmail from '@salesforce/apex/ContactController.updateEmail';Importing a method does not call it. Nothing goes to the server until your code runs the function. The next chapter covers calling it from an event handler.
Imports and cacheable methods
The same import syntax works whether or not the method has cacheable=true. The difference appears when you call it. A cacheable method can be used with @wire or imperatively. A method that is not cacheable can only be called imperatively. A later chapter covers this choice in detail.
Exercise
Write the import statement for a method named getAccounts in an Apex class named AccountService, in an org with no namespace. Then name two reasons the import might fail at deploy time.
Show answer
import getAccounts from '@salesforce/apex/AccountService.getAccounts'; The import can fail if the class or method name is misspelled, or if the method is not a public or global static method with @AuraEnabled. It can also fail if the class has not been deployed to the org.