{"id":2470,"date":"2020-05-16T03:05:14","date_gmt":"2020-05-16T08:35:14","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=2470"},"modified":"2022-01-21T11:43:57","modified_gmt":"2022-01-21T11:43:57","slug":"call-apex-method-in-lightning-web-components","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-apex-method-in-lightning-web-components.html","title":{"rendered":"How To Call Apex Method In Lightning Web Components In Salesforce"},"content":{"rendered":"<p>One of the most important parts of development is fetching data from the server, so as in Lighting Web Component development in order to fetch data from the server we need to call our Apex classes sometimes. In this blog, we will learn how to call Apex in LWC.<\/p>\n<p>We can do this in multiple ways.<\/p>\n<ul>\n<li>1. Using wire service to receive data stream<\/li>\n<li>2. Call a method imperatively<\/li>\n<\/ul>\n<p>First of all in order to expose any apex method in the lightning web component we have to follow the below steps.<\/p>\n<ul>\n<li>1. Annotate the method with @AuraEnabled . Use it on method level<\/li>\n<li>2. The Method should be static<\/li>\n<li>3. Should be public or global<\/li>\n<\/ul>\n<h4>Also Read: <a href=\"http:\/\/How to Implement Salesforce SSO Using OAuth\" target=\"_blank\" rel=\"noopener\">How to Implement Salesforce SSO Using OAuth<\/a><\/h4>\n<p>After creating this apex class import it in the javascript file of your component using the below line<br \/>\nimport your Method from \u2018@salesforce\/apex\/Namespace.Classname.yourMethod\u2019;<br \/>\nNow let\u2019s understand one by one with example.<br \/>\nEx :<br \/>\n<code class=\"d-block\">@AuraEnabled(cacheable=true)<br \/>\npublic static List getRecord() {<br \/>\n\/\/return records;<br \/>\n}<br \/>\n<\/code><\/p>\n<h2>Using Wire service<\/h2>\n<p>Syntax is:<br \/>\n<code class=\"d-block\">import apexMethod from '@salesforce\/apex\/Namespace.Classname.apexMethod';<br \/>\n@wire(apexMethod, { apexMethodParams })<br \/>\npropertyOrFunction;<br \/>\n<\/code><\/p>\n<p>Let\u2019s understand this with an example where we want to fetch contact list in LWC<\/p>\n<h4>In Component js file we can have<br \/>\n<code class=\"d-block\">import { LightningElement, wire } from 'lwc';<br \/>\nimport getContactList from '@salesforce\/apex\/ContactHelper.getContactList ';<br \/>\n<!-- attributes --><br \/>\nexport default class ContactListLWC extends LightningElement {<br \/>\n@wire(getContactList) contacts;<br \/>\n}<br \/>\n<\/code><br \/>\nLearn More: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/pipedrive-salesforce-integration.html\" target=\"_blank\" rel=\"noopener\">How To Connect Pipedrive and Salesforce Integration?<\/a><\/h4>\n<p>Here &#8211;<br \/>\nContactHelper- Apex class name<br \/>\ngetContactList \u2013 Method Name<br \/>\nIn the Component HTML file<br \/>\n<code class=\"d-block\">&lt;template&gt;<br \/>\n&lt;lightning-card title=\"Contacts From Apex\" icon-name=\"custom:custom63\"&gt;<br \/>\n&lt;div class=\"slds-m-around_medium\"&gt;<br \/>\n&lt;template if:true={contacts.data}&gt;<br \/>\n&lt;template for:each={contacts.data} for:item=\"con\"&gt;<br \/>\n&lt;p key={con.Id}&gt;{con.Name}&lt;\/p&gt;<br \/>\n&lt;\/template&gt;<br \/>\n&lt;\/template&gt;<br \/>\n&lt;template if:true={contacts.error}&gt;<br \/>\n{contacts.error}<br \/>\n&lt;\/template&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/lightning-card&gt;<br \/>\n&lt;\/template&gt;<\/code><br \/>\nHere in line &lt;template if:true={contacts.data}&gt; contacts is a variable that you have declared in your js file and bind with the data stream. It can have 2 index. One is data in case of success from apex call and second is an error in case of an error in Apex call.<\/p>\n<p>Similarly, you can wire a function too and your function can return data and error as well.<\/p>\n<div class=\"center-imgs\"><a href=\"https:\/\/multisitelocal.ezxdemo.com\/contact-us.html?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=emizen_blog\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-2389 size-full\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/hire-salesforce-developers-1.png\" alt=\"hire salesforce developers\" width=\"1000\" height=\"200\" \/><\/a><\/div>\n<h2>Call a Method Imperatively<\/h2>\n<p>In your component js file:<br \/>\n<code class=\"d-block\">import { LightningElement, track } from 'lwc';<br \/>\nimport getContactList from '@salesforce\/apex\/ContactHelper.getContactList';<br \/>\nexport default class ContactListLWC extends LightningElement {<br \/>\n@track contacts;<br \/>\n@track error;<br \/>\nhandleLoad() {<br \/>\ngetContactList()<br \/>\n.then(result =&gt; {<br \/>\nthis.contacts = result;<br \/>\n})<br \/>\n.catch(error =&gt; {<br \/>\nthis.error = error;<br \/>\n});<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\nHere getContactList is a method written in apex class.<\/p>\n<p>In your HTML file use, this contacts variable.<br \/>\n<code class=\"d-block\">&lt;template&gt;<br \/>\n&lt;lightning-card title=\"Contact List ImperativeCall\" icon-name=\"custom:custom63\"&gt;<br \/>\n&lt;div class=\"slds-m-around_medium\"&gt;<br \/>\n&lt;p class=\"slds-m-bottom_small\"&gt;<br \/>\n&lt;lightning-button label=\"Load Contacts\" onchange={handleLoad}&gt;&lt;\/lightning-button&gt;<br \/>\n&lt;\/p&gt;<br \/>\n&lt;template if:true={contacts}&gt;<br \/>\n&lt;template for:each={contacts} for:item=\"con\"&gt;<br \/>\n&lt;p key={con.Id}&gt;{con.Name}&lt;\/p&gt;<br \/>\n&lt;\/template&gt;<br \/>\n&lt;\/template&gt;<br \/>\n&lt;template if:true={error}&gt;<br \/>\n{error}<br \/>\n&lt;\/template&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/lightning-card&gt;<br \/>\n&lt;\/template&gt;<br \/>\n<\/code><br \/>\nThis is all about calling Apex in Lightning Web Components.<\/p>\n<p>Emizentech is a reliable <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce-consulting.html\">salesforce consulting company<\/a> and our experienced <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce.html\">salesforce developers<\/a> can provide you professional assistance for your next salesforce project.<\/p>\n<h4>Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-salesforce-lightning-map-component.html\" target=\"_blank\" rel=\"noopener\">How to Create a Salesforce Lightning Map Component?<\/a><\/h4>\n<p><b>Author Bio:<\/b><\/p>\n<p>With a decade of experience in <a href=\"https:\/\/multisitelocal.ezxdemo.com\/ecommerce-development.html\">eCommerce technologies<\/a> and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients\u2019 IT infra structure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business process and enhance productivity. Let\u2019s Talk.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most important parts of development is fetching data from the server, so as in Lighting Web Component development in order to fetch data from the server we need to call our Apex classes sometimes. In this blog, we will learn how to call Apex in LWC. We can do this in multiple<\/p>\n","protected":false},"author":39,"featured_media":2596,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-2470","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-salesforce-development"},"modified_by":"emizentech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/Apex-call-in-LWC-1.png","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/Apex-call-in-LWC-1.png","author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/salesforce"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=2470"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2470\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/2596"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=2470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=2470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=2470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}