Tag: Invoking Apex Methods In Lightning web components

Call Apex Methods In Lightning web components

Let’s discuss here how to call the apex class from the Lightning web components. Lightning web components can import methods from Apex classes into the JavaScript classes using ES6 import. Once after importing the apex class method you can able call the apex methods as functions into the component by calling either via the wire service or imperatively. Before you use an Apex method, make sure that there isn’t an easier way to get the data. See whether a base Lightning component, like lightning-record-form, lightning-record-view-form, or lightning-record-edit-form works for your use case. If they don’t give you enough flexibility, use a wire adapter like getListUi or getRecordUi. 

Refer this link for how to configure the Salesforce dx for web components 

Import Syntax 

You can able use default export syntax to import an Apex method via the @salesforce/apex scoped module into JavaScript controller class. The Syntax looks like below.

apexMethod—The imported symbol that identifies the Apex method.
Namespace—The namespace of the Salesforce organization. Specify a namespace unless the organization uses the default namespace (c), in which case don’t specify it.
Classname—The name of the Apex class.

Create Apex Class 

In this example, we will be getting account data and show it into the UI. Create an apex class using SFDX create apex class command.

Here is the apex class. 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

Now you can able to call the apex class in  Lightning web component using these different ways.

  • Wire a property
  • Wire a function
  • Call a method imperatively

Wire an Apex Method to a Property

If an Apex method is annotated with @AuraEnabled(Cacheable=true), you can invoke it from a component via the wire service. You can @wire a property or a function. Here is the syntax

Create a Lightning web component using below SFDX commands

Here is the LWCWireEx.html markup for the lightning web components.

Here is the LWCWireEx.js class

Here is the LWCWireEx.js-meta.xml markup.

Push the changes to scratch org and add the lightning web component to the record page. You can able to see the result like below.

Wire an Apex Method to a Function

Now let’s look at how to wire an apex method to function.we will be updating the same code here to operate the apex method as function.

Update the LWCWireEx.js class as shown below

update the LWCWireEx.html markup as shown below

Push the changes to scratch org and add the lightning web component to the record page. You can able to see the result like below.

 

Call an Apex Method Imperatively

Now let’s see here how to call apex method imperatively. Create a new Lightning web component using the below SFDX command

Use the below ImperativEx.html code

Use the below ImperativEx.js class code

Use the below ImperativEx.js-meta.xml class code

push the changes to scratch org and add the lightning web component to the record page and you can able to see the below result when you click on the button .

Lightning Web Components Render List

One of the most common use cases is to iterate through the collection when you are working on data collection. Let’s discuss here what all the different ways to do it in lightning web components. To render a list of items, use for each directive or the iterator directive to iterate over an array. Add the directive to a nested <template> tag that encloses the HTML elements you want to repeat. Please refer this link for web component environment setup

Using for:each directive

Let us discuss here how to use the for:each. When using the for:each directive, use for:item=”currentItem” to access the current item. To assign a key to the first element in the nested template, use the key={uniqueId} directive. Regardless of which directive you use, you must use a key directive to assign a unique ID to each item. When a list changes, the framework uses the key to rerender only the item that changed. The key must be a string or a number, it can’t be an object. You can’t use the index as a value for the key. Assign unique keys to an incoming data set. Here is the simple apex class that will get the data from the account object and its contacts. create an apex class using the below  SFDX command.

Here is the complete code.

Below is the lightning web component that will iterate over an array called accounts, which is defined in the component’s JavaScript class.  Run the following SFDX command to create a web component

CollectionExample.html

the above markup is iterating the data using the for:each directive as like below.

CollectionExample.js

CollectionExample.js-meta.xml

Now push the component changes to scratch org and add this component to the record page. You can able to see the collection iteration data as shown below.

 

Nested Iteration using for:each directive

Now let us iterate the nested loop using the for:each directive .here is the example that will render the account and its contacts. Update the CollectionExample.html markup with the below code and push the changes to scratch org.

Push the change to scratch org and reload the page. Here is the final output.

 

Using Iterator directive

Iterator directive is another way to render the collection data. To apply a special behavior to the first or last item in a list, use the iterator directive, iterator:iteratorName={array}. Use iteratorName to access these properties:

  1. value—The value of the item in the list. Use this property to access the properties of the array. For example, iteratorName.value.propertyName.
  2. index—The index of the item in the list.
  3. first—A boolean value indicating whether this item is the first item in the list.
  4. last—A boolean value indicating whether this item is the last item in the list.

This sample code uses the same array as the previous example. To apply special rendering to the first and last items in the list, the code uses the first and last properties with the if:true directive. Update the  CollectionExample.html code with below code . in this example we will be displaying the list of accounts.

CollectionExample.css 

Push the changes to scratch org and you can able to see these changes after reload.

Now let us see how to do the nested iteration using iterator directives . in this example we will be displaying accounts and its contacts. here is the HTML markup for the nested iteration.

 

Push the changes to the scratch org and reload the page. you can able to see the output as shown below and will be displayed account and its contacts.