This blog will help you learn how to call the apex method in the lightning web component. To perform the functions written in the apex class to perform a specific task and create new business solutions.
There are two ways to call Apex method from Lightning Web Component:
- Call apex method Using Wire services.
- Call apex method imperatively.
Calling apex method using wire services in lightning web component:
Using Wire method:
To call the apex method in the lightning web component, First, we have to create the apex class and add the @AuraEnabled method at the first line, i.e., before starting the method. To call it from Wire Service, the method should be cacheable. Hence, add cacheable=true in @AuraEnabled. And in the LWC js controller, we need to write the import method using the @salesforce/apex/className.MethodName; at the start of the js controller and in the lightning, we need to write the @wire to get the records we provide in the apex class.
Using Imperatively method:
Calling the apex method in the lightning web component without using the wire method is more accessible than the wire method. Making the AuraEnabled method cacheable is not mandatory to call it imperatively. While using the wire method, we need to add the data, but there is no need for this in the imperatively method. It is the easiest way to call the apex method in the LWC.
For Ex:-
Apex class with the @AuraEnabled:-
In JS Controller, we need to import this method using @salesforce/apex/ module like below:
For the wire method:
To call imperatively:
The above classes and the code will give you a brief idea of calling the apex method in the lightning web component. And using the methods, we can quickly call the apex method and get the data to perform the particular tasks.
We hope that you find this blog helpful, if you still have queries, don’t hesitate to contact us at salesforce@greytrix.com.
About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.
Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.
The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.
For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.
Related Posts
- Create records from Apex Restful Service
- How To Create A Quote Template In Salesforce?
- How to Use Email Template in Apex Code
- Clone Record of Any Object using Flows in Salesforce
- How to Avoid Null Pointer Exception By Using Safe Navigator Operator (?.)
- How to create a dynamic multi-filter object in Salesforce-Part I