Calling Apex method from LWC
In this blog post, we will take a look at the steps to call an Apex method from our Lightning Web Component with source code and demonstration videos.
There are the following ways to call an Apex method into LWC:
1. Calling an Apex method using wire services.
2. Calling an Apex method imperatively.
3. Calling and Apex method with async and await.
Now let's take a look at the steps involved in the above-mentioned ways.
Calling Apex method using Wire services
The wire service in LWC helps in the retrieval of data or performing any action without the need for Apex code. You can use the wire service, to wire a property or a function that will receive data when the source is updated.
1. Expose the Apex method which you want to call from LWC
The Apex method should be:
a. Global or Public.
b. decorated with @AuraEnabled, if the method has (cacheable=true) after this annotation, the results are cached at the client site to improve performance.
2. Import the Apex method which you want to call from LWC
To call an Apex method you need to import it in your Lightning Web Component's JS file.
- apexMethod: this specifies the apex method in your JS file, usually I use the same name as the method name to avoid any confusion.
- namespace: if the apex class is a part of a managed package specify the namespace of the managed package.
- className: this specifies the name of the Apex class.
- apexmethodName: this specifies the name of the apex method that you want to call from LWC.
3. Wire the Apex method to LWC
In LWC you can use @wire adapter to either wire a JS function or property to your Apex method.
Now let's take a look at an example where we are calling two apex methods using wire service from LWC -
Apex Class
HTML
JavaScript
Output (Apex method call without parameter)
Demonstration Video (Apex method call with parameter)
Calling an Apex method imperatively
When you want to have control over the execution of the Apex method invocation (for example - in response to the clicking of a button) use the imperative approach.
As per Salesforce, this approach should be used in the following scenarios:
- To call Apex methods that aren't annotated with cacheable=true, which includes methods performing any DML.
- To control when the method is invoked in LWC.
- To work with objects that arent' supported with User Interface API such as - Task and Event.
- To call a method from the ES6 module that doesn't extend Lightning Element.
Now let's take a look at an example where we are calling two apex methods from LWC using the imperative approach -
Apex Class
HTML
JavaScript
Demonstration Video
Calling an Apex method using async and await
You can call Apex methods from LWC asynchronously, using async and await keywords in JS.
In this approach:
- The Async function always returns Promises.
- The Await function stops and waits for the result of the Async function before going further.
Now let's take a look at an example where we are calling two apex methods from LWC using the async await approach -
Apex Class
HTML
JavaScript
Demonstration Video
Thank you for reading the blog.
You can share your thoughts with me by dropping a message to me on LinkedIn
You can find my other blogs on my blog page - LWC - Blog
Comments
Post a Comment