Skip to main content

Connect LWC with Apex Class and Play With Salesforce Data


Agenda

I am Writing This Blog To share my knowledge with everyone and we can learn

about something in Lightning Web Component. And In this Blog, we are going to

learn different ways to connect a Lightning Web Component (LWC).

Blog Details

This Blog gives us a brief about What is a Lightning Web Component, and how

can we connect a Lightning Web Component with the Apex Class and or an Apex

Method and how can we play with salesforce data in our way.


What we are going to do

We are going to create a Lightning Web Component using VS-Code. And Use the

JavaScript of that file to connect with Apex Class Method. For that, we use different

ways to establish a connection between apex class and LWC and play with the data.


Requirements:

  • You need to have access to your org.

  • Should have installed the VS Code Installed in your system.
  • You should have some basic knowledge of Lightning Web Component.


A Little About Lightning Web Component(LWC)


Lightning Web Components is open-source, empowering you

to explore the source code, customize the behavior for

your needs, and build enterprise-ready web components on any platform, not just Salesforce.



Lightning Web Component or LWC are custom HTML elements built using HTML
and modern JavaScript. We Basically work on ES6(ECMAScript 2015). LWC uses
core Web Components standards and delivers exceptional performance.

We can Connect the JavaScript file of the component to establish a connection with

salesforce data. There are different ways to do it which are as follows:


  • Using Wire Services

  • Lightning Data Service

  • Using the Base Components (Lightning-Record-Form)

  • Calling API’s From Apex

  • By Long-Running CallOut’s with Continuation


And In that blog, we use Wire Services and see it’s working.

Lightning web components can import methods from Apex classes. The imported methods

are functions that the component can call either via @wire or imperatively.


Syntax





To call an Apex method, a Lightning web component can:

  • Wire a property

  • Wire a function

  • Call a method imperatively



Make The Apex Methods available to use in Lightning Web Component:

To expose an Apex method to a Lightning web component, the method must be static

and either global or public. Annotate the method with @AuraEnabled.





To set cacheable=true, a method must only get data, it can’t mutate (change) data.





Call Apex Method Imperatively or from another JavaScript

Call a method imperatively when you must control when the invocation occurs

(for example, in response to clicking a button, or to delay loading to outside the critical path).

We Can call an Apex Method in both ways like imperatively or via @wire only

when a 

A method is marked with @AuraEnabled(cacheable = true).

Refresh Apex After every Callout 

Because whenever the component inserted in the DOM the Data needed to be

refreshed and rendered on the HTML. And for this purpose, we use an already

existed wired function called refreshApex.

You can import refreshApex from @salesforce/apex to query for updated data

and refresh the cache of a wired property. 



And It Will Automatically Refresh Apex after every wired method execution.


And We can also call the Refresh method on button click or from another Java

Script method by using that refreshApex in a method like :




Blog’s Conclusion:

So, the conclusion of the blog is that now we know how to connect a Lightning

Web Component (LWC) with the Apex Class and how can we access Apex class

Method’s and Variables and Use it inside our Lightning Web Component (LWC)

and play with Salesforce data.


And I want you to get familiarized with the Wired method as well as the refreshApex

method.


Thanks, And Regards

Rahul Vaishnav


Comments

Post a Comment

Popular posts from this blog

Using Data - Attributes in Lightning Web Component (LWC)`

  Agenda      While working with Salesforce data we need to play with Id’s (Identifiers). An ID can be associated with a Record or with a specifier. While working with records when we need to make changes or view any particular record details, we need their ID’s, as I describe in my previous blog’s how can we show or edit any salesforce record with the help of Basic components of Salesforce.  To create forms that let users view, edit, and create Salesforce records, use Lightning-record-form, Lightning-record-edit-form, and Lightning-record-view form components. And To use these components we need Salesforce Record ID. Blog Details This Blog gives us a brief about how can we get a particular record Id or value after any event using Data - Attributes.  What we are going to do In this blog, we use Data - Attribute functionality from HTML5  in our Lightning Web Component and use data attribute to get...

Select - Deselect checkbox in Lightning Web Component

  Agenda In the previous blog, we are working with a single record and manipulating it as per the requirements, but when we are working for the organization we need to think to cover larger aspects. And while working with salesforce data we should be aware of the fact that we might need to work with data in bulk. So, when we need to work with more than one record we use checkbox functionality and use that checkbox to see which record we need to make changes to. Blog Details The blog gives us a brief that how we select and unselect all checkboxes from the header checkbox. What we are going to do In this blog, we use a table to show the records with the checkbox for each record and a header checkbox to select - unselect all records. Requirements: You need to have access to your org.     Should have installed the VS Code Installed in your system.    You should have some basic knowledge of Lightning Web Component. You must have some proper records ...

Displaying User Level’s Dynamically In Lightning Web Component

  Agenda I am writing this blog, to display dynamic user data beautifully, It enables each user to see the users they need to report. With this dynamic component, you can control data visibility without having to create a separate component as well, with its own running user.  Now, It’s time to show the user’s of our organization dynamically their role in a beautiful way. How does it work? You fetch the User’s record with a simple Apex SOQL query and just displaying details Dynamically over a Lightning Web Component, And place that component where ever you want. Requirements: You need to have access to your org. Should have the VS Code Installed in your system. You should have some basic knowledge of Lightning Web Component. About Lightning Web Component (LWC): Lightning Web Components are lightweight custom elements based on native HTML and Modern JavaScript. Lightning Web Compone...