{"id":6584,"date":"2022-01-31T04:14:18","date_gmt":"2022-01-31T04:14:18","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6584"},"modified":"2024-09-04T05:55:45","modified_gmt":"2024-09-04T05:55:45","slug":"how-to-call-the-apex-method-in-lightning-web-component","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/31\/how-to-call-the-apex-method-in-lightning-web-component\/","title":{"rendered":"How to call the apex method in lightning web component"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p><strong>There are two ways to call Apex method from Lightning Web Component:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Call apex method Using Wire services.<\/li>\n\n\n\n<li>Call apex method imperatively.<\/li>\n<\/ol>\n\n\n\n<p>Calling apex method using wire services in lightning web component:<\/p>\n\n\n\n<h1 class=\"wp-block-heading has-vivid-red-color has-text-color has-link-color wp-elements-a69aa8549c53e0b8350d503ce864db0b\"><strong>Using Wire method:<\/strong><\/h1>\n\n\n\n<p>To call the apex method in the lightning web component, First, we have to create the apex class and add the <strong>@AuraEnabled <\/strong>method at the first line, i.e., before starting the method. To call it from&nbsp;<strong>Wire Service<\/strong>, the method should be&nbsp;<strong>cacheable<\/strong>. Hence, add&nbsp;<strong>cacheable=true<\/strong>&nbsp;in&nbsp;<strong>@AuraEnabled<\/strong>. And in the <strong>LWC<\/strong> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-vivid-red-color has-text-color has-link-color wp-elements-f2a1c9994196e21d3d86800924d18667\"><strong>Using Imperatively method:<\/strong><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>For Ex:-<\/p>\n\n\n\n<p>Apex class with the @AuraEnabled:-<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/1_AccountController_apexclass.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/1_AccountController_apexclass.png\" alt=\"AccountController_apexclass\"><\/a><\/center>\n<font size=\"2\"><center><i>AccountController_apexclass<\/i><\/center><\/font>\n\n\n\n<p>In JS Controller, we need to&nbsp;<strong>import&nbsp;<\/strong>this method using&nbsp;<strong>@salesforce\/apex\/<\/strong>&nbsp;module like below:<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/2_JS-controller-import-method..png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/2_JS-controller-import-method..png\" alt=\"JS controller import method\"><\/a><\/center>\n<font size=\"2\"><center><i>JS controller import method<\/i><\/center><\/font>\n\n\n\n<p>For the wire method:<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/3_wire-method.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/3_wire-method.png\" alt=\"wire method\"><\/a><\/center>\n<font size=\"2\"><center><i>wire method<\/i><\/center><\/font>\n\n\n\n<p>To call imperatively:<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/4_Imperatively-method.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/4_Imperatively-method.png\" alt=\"Imperative method\"><\/a><\/center>\n<font size=\"2\"><center><i>Imperative method<\/i><\/center><\/font>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>We hope that you find this blog helpful, if you still have queries, don\u2019t hesitate to contact us at  <a rel=\"noreferrer noopener\" href=\"mailto:salesforce@greytrix.com\" target=\"_blank\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 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.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, 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&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; 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.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/17\/how-to-create-records-from-apex-restful-service-in-salesforce\/\" data-type=\"URL\" data-id=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/17\/how-to-create-records-from-apex-restful-service-in-salesforce\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create records from Apex Restful Service<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/10\/how-to-create-a-quote-template-in-salesforce\/\" target=\"_blank\">How To Create A Quote Template In Salesforce?<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/09\/22\/how-to-use-email-template-in-apex-code\/\" target=\"_blank\">How to Use Email Template in Apex Code<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/16\/clone-record-of-any-object-using-flows-in-salesforce\/\" target=\"_blank\">Clone Record of Any Object using Flows in Salesforce<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/14\/how-to-avoid-null-pointer-exception-by-using-safe-navigator-operator\/\" target=\"_blank\">How to Avoid Null Pointer Exception By Using Safe Navigator Operator (?.)<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/17\/how-to-create-a-dynamic-multi-filter-object-in-salesforce-part-i\/\" target=\"_blank\">How to create a dynamic multi-filter object in Salesforce-Part I<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: Calling apex method using wire services in\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/31\/how-to-call-the-apex-method-in-lightning-web-component\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":9433,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[13,859,14,15,16,18,20,1254,1256,1252,1049,367,1251,1253,1257,1255],"class_list":["post-6584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce-srv","tag-apex","tag-apex-class","tag-apex-classes","tag-apex-development","tag-apex-in-salesforce","tag-apex-methods","tag-apex-references-in-salesforce","tag-imperative-method","tag-imperative-method-in-apex-class","tag-imperatively","tag-lwc","tag-salesforce","tag-wire","tag-wire-method","tag-wire-method-in-apex-class","tag-wire-method-in-apex-method"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6584"}],"version-history":[{"count":3,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6584\/revisions"}],"predecessor-version":[{"id":9434,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6584\/revisions\/9434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media\/9433"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}