StandardSetController in Lightning Web component for List View : sanketthoughts

StandardSetController in Lightning Web component for List View
by: sanketthoughts
blow post content copied from  Salesforce Diaries
click here to view original post


Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.

Workaround – Gettin record Ids of selected record from standard list view in Lightning Web Component via Lightning Flow

We are going to create one Lightning Flow where our Lightning Web Component will be called inside it. The Lightning Flow will be invoked via URL button on standard list view.

The Idea of getting selected record Ids from Standard List View is basically derived from this Blog. My solution extends it from Flow to Lightning Web Component.

Create Lightning Web Component – massActionViaFlow

We are going to create Lightning Web Component with name massActionViaFlow which can invoked from Lightning Flow.

massActionViaFlow.html

We are iterating over the array of ids received from Lightning Flow using for:each iterator.

<template>
    <lightning-card title="HelloForEach" icon-name="custom:custom14">
        <ul class="slds-var-m-around_medium">
            <template for:each={ids} for:item="id">
                <li key={id}>{id}</li>
            </template>
        </ul>
    </lightning-card>
</template>

massActionViaFlow.js

The Lightning Web Component receives the ids as string with each value comma separated. The ids are converted into an array of Ids from String using split method and filter method in JavaScript inside connectedCallback method.

import { LightningElement, api } from 'lwc';

export default class MassActionViaFlow extends LightningElement {
    @api ids;
    @api objectApiName;
    connectedCallback() {
        if (this.ids) {
            this.ids = this.ids.split(',').filter(function (e) {
                return e != null && e != '';
            });
        }
    }
}

massActionViaFlow.js-meta.xml

The xml file of the component uses target as

lightning__FlowScreen and exposed the ids property to flow so that it can be set from their.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>51.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
      <target>lightning__FlowScreen</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__FlowScreen">
            <property name="ids" label="ids" type="String" />
            <property name="objectApiName" label="objectApiName" type="String" />
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

Create Lightning Flow – List View Mass Action

We are going to create flow with Name List View Mass Action. The flow will have below variables:-

  1. ids – It holds all the selected record Ids value in array
  2. RecordIds – This holds all the record ids separated by comma.
  3. objectName – It is optionally created.

You need to follow the screenshots while creating variables:-

Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.
Variable Name – ids
Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.
Variable Name – RecordIds
Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.
Variable Name – objectName

It’s time to add the flow elements. ids variable will automatically hold the value of selected record in the form of array. We are going to run a for loop in flow so that we can create a string with all record ids separated by comma. The Lightning Flow will look like this:-

Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.

Element – IterateOverIds

Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.

For Loop Assignment

Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.

Screen Element – Lightning Web Component called here

We need to pass the value of public property of Lightning Web Component from Flow. We have already prepared one string variable to hold the record Ids.

Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.

It’s time to save your flow and activate it. Now, all you have to do is create a list button of URL type. The URL will be :-

/flow/List_View_Mass_Action?objectName=Account&retURL=/lightning/o/Account/home

The objectName variable value is being passed via URL and It can be used within Flow or can be passed to Lightning Web Component so that the solution can be extended. retURL in the URL of the button defines the finish behaviour of the flow. In this case, On click of finish button, Users will be navigated to list view of Account object.
Visualforce comes with a very powerful feature StandardSetController class which gives you the ability to fetch records selected by end user on standard list view. This is a limitation in Lightning Web Component but there is a workaround to get the record Ids of the record selected by end user on standard list view with the help of Lightning Flow and List View URL Button in Salesforce and then pass it to Lightning Web Component.

Demoselected record Ids in Lightning Web Component in List Views

The post StandardSetController in Lightning Web component for List View appeared first on Salesforce Diaries.


April 11, 2021 at 05:06PM
Click here for more details...

=============================
The original post is available in Salesforce Diaries by sanketthoughts
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================