Saturday, May 7, 2022

Decorators in LWC

 

The Lightning Web Components programming model has three decorators that add functionality to property or function. Decorators dynamically alter the functionality of a property or function.

There are mainly three decorators that we use in LWC.

@api:

     Public properties are reactive. If the value of public property changes, the component rerenders.

     To expose a public property, decorate a field with @api. Public properties define the API for a component.

·      To expose a public method, decorate it with @api. Public methods are part of a component’s API. To communicate down the containment hierarchy, owner and parent components can call JavaScript methods on child components.

·         import @api decorator from lwc

1

import { LightningElement, api } from 'lwc';

doItem.js

1

2

3

4

5

// todoItem.js

import { LightningElement, api } from 'lwc';

export default class doItem extends LightningElement {

    @api itemName = 'New Item';

}

todoItem.html

1

2

3

4

5

<!-- todoItem.html -->

<template>

    <div class="view">

        <label>{itemName}</label>

    </div>

todoApp.html

1

2

3

4

5

6

7

<!-- todoApp.html -->

<template>

    <div class="listing">

        <c-todo-item item-name="Srinivas"></c-todo-item>

        <c-todo-item item-name="Lakshmi"></c-todo-item>

    </div>

</template>

 

@track

Fields are reactive. If a field’s value changes, and the field is used in a template or in a getter of a property that’s used in a template, the component rerenders and displays the new value.

There is one use case for @track. When a field contains an object or an array, there’s a limit to the depth of changes that are tracked. To tell the framework to observe changes to the properties of an object or to the elements of an array, decorate the field with @track

Import @api decorator from lwc.

helloWorld.html

1

2

3

4

5

6

7

8

<template>

    <lightning-card title="HelloWorld" icon-name="custom:custom14">

      <div class="slds-m-around_medium">

        <p>Hello, {greeting}!</p>

        <lightning-input label="Name" value={greeting} onchange={changeHandler}>

       </lightning-input>

      </div>

    </lightning-card>

</template>

helloWorld.js

1

2

3

4

5

6

7

import { LightningElement } from 'lwc';

export default class HelloWorld extends LightningElement {

  greeting = 'World';

  changeHandler(event) {

    this.greeting = event.target.value;

  }

}

 @wire

To read Salesforce data, Lightning web components use a reactive wire service. When the wire service provisions data, the component rerenders.

 Components use @wire in their JavaScript class to specify a wire adapter or an Apex method.

We need to import the @salesforce/apex scoped module into JavaScript controller class.

1

import apexMethodName from '@salesforce/apex/Namespace.Classname.apexMethodRef';

 We can call the apex class in  Lightning web component using these different ways:

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

Friday, May 6, 2022

Data Binding

 Data binding is a technique, where the data stays in sync between the component and the view.

Whenever the user updates the data in the view, LWC updates the component. When the component gets new data, the LWC updates the view.

Data Binding is of  two  types:

One-way Data Binding:

One-way data binding is a situation where information flows in only one direction in our case from the controller to the template(HTML).

Two-Way Binding:

Two-way data binding in LWC will help users to exchange data from the controller to the template and form template to the controller.

 It will help users to establish communication bi-directionally.

  • The Lightning Web Components programming model has given us some decorators that add functionality to property or function.
  • @track is the decorator that helps us to track a private property's value and re-render a component when it changes.
  • Tracked properties are also called private reactive properties.
  • @track helps us to achieve two-way data binding

Track a property only if you want the component to re-render when the property's value changes.

Note: Don't track every private property.

 

Saturday, April 30, 2022

Salesforce LMS


Lightning Message Service (LMS) is the Salesforce technology of its kind that facilitates communication between Visualforce, Lightning Web Components, and Aura Components on a Salesforce Lightning Page.



 With the launch of LMS, Salesforce developers obtained a simple and seamless API for publishing messages throughout the Lightning Experience platform and subscribed to messages that originated from anywhere within Lightning Experience.

·         In LWC we can access Lightning Message Channel with the scoped module @salesforce/messageChannel.

·         In Visualforce, we can use global variable $MessageChannel.

·         In Aura, use lightning:messageChannel in your component

Create Lightning Message Channel

Currently we can create Lightning Message channel with Metadata API. You can create the same with the help of VsCode.

  You need to create one DX project then you need to place your message channel definition with the suffix .messageChannel-meta.xml in the force-app/main/default/messageChannels directory. like below folder structure.



 Sample Message-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningMessageChannel xmlns="https://soap.sforce.com/2006/04/metadata">

    <masterLabel>Counter</masterLabel>

    <isExposed>True</isExposed>

    <lightningMessageFields>

        <fieldName>operator</fieldName>

    </lightningMessageFields>

    <lightningMessageFields>

        <fieldName>constant</fieldName>

    </lightningMessageFields>

</LightningMessageChannel>

Import a Message Channel:

Here’s how to import a Lightning message channel that a component can use to communicate via the Lightning Message Service.

// Syntax

import channelName from '@salesforce/messageChannel/channelReference';

// Syntax for resources in a managed package

import channelName from '@salesforce/messageChannel/namespace__channelReference';

// Example

import recordSelected from '@salesforce/messageChannel/Record_Selected__c';

channelName—An imported symbol that identifies the message channel.

channelReference—The API name of the message channel.

namespace—If the message channel is in a managed package, this value is the namespace of the managed package. If the message channel is not in a managed package, do not include a namespace.

Restrictions in LMS:

Here are several restrictions to the Lightning Message Service.

 >Under the following circumstances, you are unable to utilise LMS:

· Salesforce Mobile

· Communities

· AppExchange

Note:

LMS cannot be used to communicate with VF page contained in an iframe in Aura/LWC.

Examples of data that you can pass in a message include strings, numbers, booleans, and objects. A message cannot contain functions and symbols.

The lightning:messageChannel component is only available in Lightning Experience. 

Saturday, April 23, 2022

Custom settings vs Custom Metadata

 Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user.

All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. This data can then be used by formula fields, validation rules, flows, Apex.

Custom metadata is customizable, deployable, packageable, and upgradeable application metadata.


Monday, April 11, 2022

Salesforce Source-driven Development

 In source-driven development, the source is organized into artifacts based on group features or customizations that are delivered together. Salesforce DX incorporates the artifact-based approach to organizing the source. 

There is the ability to shift the source of truth from the org to a version control system (VCS), along with the use of popular collaboration technologies including Git and other test-build automation tools.

A Salesforce DX project is a local directory structure of your metadata in source format. It lets you develop and test with Salesforce DX tooling.

Salesforce DX allows developers to use Git and source repositories when doing any sorts of development on Salesforce platform.

Scratch orgs are a central feature of Salesforce DX. The Scratch org is a source-driven and disposable deployment of Salesforce code and metadata.

Dev Hub allows you to create and manage scratch orgs. It is also essential create and manage second-generation packages, and use Einstein features.




ES12 new Features