Update lookup field(custom lookup) value using flow through Quick Action

Update lookup field value using flow through Quick Action


Below are the steps to follow,

1. Create one ‘screen flow’ and add one screen in screen flow.

2. Create Custom LWC for custom lookup.

3. Display custom field in lookup (for displaying address field in lookup) and display the associated Account by default in lookup field.

file.html


<template>

    <div class="slds-form-element">

        <div class="slds-form-element__control" style="text-align: left !important;">

            <p class="slds-form-element__label">

                Account

            </p>

            <div class="slds-combobox_container" aria-expanded="true" aria-haspopup="listbox" role="combobox">

                <div class="slds-combobox slds-dropdown-trigger slds-dropdown-trigger_click slds-is-open" >

                    <template if:false={showPillAfter}>

                        <template if:false={showPillBefore}>

                            <lightning-input

                                name="enter-search"

                                type="search"

                                placeholder="Select Accounts..."

                                class="inputBox"

                                onchange={handleChange}

                                autocomplete="off"

                                variant="label-hidden">

                            </lightning-input>


                        <template if:true={records}>

                            <div id="listbox-id-2" class="slds-dropdown slds-dropdown_length-with-icon-7 slds-dropdown_fluid" role="listbox">

                                <ul class="slds-listbox slds-listbox_vertical" role="presentation">

                                <lightning-spinner alternative-text="Loading" size="medium" if:true={loading}></lightning-spinner>

                                    <template for:each={records} for:item="i" for:index="index" if:false={noRecords}>

                                        <li key={i.Id} data-item={i.Id} value={index} role="presentation" class="slds-listbox__item" onclick={handleClick}>

                                            <div class="slds-media slds-listbox__option slds-listbox__option_entity slds-listbox__option_has-meta" role="option">

                                                <div class="slds-media__figure slds-listbox__option-icon">

                                                    <div class="slds-icon_container slds-icon-standard-account">

                                                        <lightning-icon 

                                                            icon-name="standard:account"

                                                            alternative-text="Account">

                                                        </lightning-icon>

                                                    </div>

                                                </div> &nbsp;&nbsp;

                                                    <div class="slds-listbox__option-text slds-listbox__option-text_entity">

                                                        {i.Name} <br>

                                                        {i.Address__Street__s} {i.Address__City__s} {i.Address__StateCode__s} {i.Address__PostalCode__s}

                                                    </div>

                                            </div>

                                        </li>

                                    </template>

                                    <template if:true={noRecords}>

                                        <li role="presentation" class="slds-listbox__item">

                                            <div class="slds-media slds-listbox__option slds-listbox__option_entity slds-listbox__option_has-meta" role="option">

                                                <div class="slds-listbox__option-text slds-listbox__option-text_entity">

                                                    No Record Found !!

                                                </div>  

                                            </div>

                                        </li>

                                    </template>

                                </ul>

                            </div>

                        </template>

                    </template>

                        

                        <div if:true={showPillBefore} class="pillContainer">

                            <lightning-pill width=100%; class="fullWidth slds-combobox_container" label={pillName} name={pillName} onremove={handleRemoveBefore}>

                                <lightning-icon 

                                    icon-name="standard:account"

                                    alternative-text="Account">

                                </lightning-icon>

                            </lightning-pill>

                        </div>

                    </template>


                    <div if:true={showPillAfter} class="pillContainer">

                        <lightning-pill width=100%; class="fullWidth slds-combobox_container" label={pillName} onremove={handleRemoveAfter}>

                            <lightning-icon 

                                icon-name="standard:account"

                                alternative-text="Account">

                            </lightning-icon>

                        </lightning-pill>

                    </div>

                </div>

            </div>

        </div>

    </div>

</template>



file.js

  • fetchAllAccount () - fetch all the accounts

  • fetchAccount() - fetch the current account record

import { LightningElement, api, track, wire } from 'lwc';

import getContact from '@salesforce/apex/customLookup.getContact';

import getAccounts from '@salesforce/apex/customLookup.getAccounts';


export default class CustomLookup extends LightningElement {

    @api recordId;

    @track recordOptions = [];

    @track fetchAcc = null;

    @track records = [];

    @api accountID;

    @api selectedRecordId;

    contactID;

    loading;

    selectedRecord;

    pillName;

    showPillBefore = false;

    showPillAfter = false;

    noRecords;

    accountList = [];

    isRenderCallbackActionExecuted = false;


    handleRecordChange(event) {

        this.selectedRecord = event.detail.value;

    }


    handleClick(event){

        this.selectedRecord =     this.records[event.currentTarget.value];

        this.showPillBefore = true;

        this.showPillAfter = false;

        this.selectedRecordId = this.selectedRecord.Id;

        this.noRecords = true;

        this.records=null;

        this.pillName = this.selectedRecord.Name;

    }


    handleRemoveBefore(){

        this.selectedRecordId ='';

        this.showPillBefore = false;

    }


    handleRemoveAfter(){

        this.selectedRecordId ='';

        this.showPillBefore = false;

        this.showPillAfter = false;

        this.isRenderCallbackActionExecuted = true;

    }


    handleChange(event){

        this.noRecords = false;

        let temp = event.target.value;

        if(temp.length > 1){

            this.loading = true;

            let accounts=[];

            this.accountList.forEach(item =>{

                let accountName = item.Name;

                if(accountName.toLowerCase().includes(temp.toLowerCase())){

                    accounts.push(item);

                }

             });

             this.loading = false;

            this.records = accounts;

        }

        else{

            this.records = null;

        }

    }


    

    connectedCallback(){

        this.fetchAllAccount();

    }


    

    renderedCallback(){

        this.contactID = this.accountID;

        if (this.isRenderCallbackActionExecuted == false) {

            this.fetchAccount();        

        }

        this.isRenderCallbackActionExecuted = true;

    }

    

    fetchAllAccount(){

        getAccounts().then(response =>{

            this.records = response;

            this.accountList = response;

        })

        .catch((error) => {

             console.log('====ERROR===>',error);       

        });

    }


    fetchAccount(){

        this.showPillAfter = true;

        this.showPillBefore = false;

        this.selectedRecordId = this.contactID;

        getContact({contactID : this.contactID}).then(response =>{

            let accName = response.Account.Name;

            let accId = response.Account.Id;

            this.pillName = accName;

            this.selectedRecordId = accId;

        })

        .catch((error) => {

             console.log('====ERROR===>',error);       

        });

    }

}



file.js-meta.xml

  • Pass selected recordID (from custom lookup to flow) using the variable ‘selectedRecordId’

  • Get ContactID (from flow to LWC) using the variable ‘contactID’

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

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>58.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__FlowScreen</target>

        <target>lightning__RecordPage</target>

    </targets>

    <targetConfigs>

        <targetConfig targets="lightning__FlowScreen">

            <property name="selectedRecordId" type="String" label="Selected Record ID" role="outputOnly"></property>

            <property name="accountID" type="String" label="Contact ID"></property>

        </targetConfig>

    </targetConfigs>

</LightningComponentBundle>



file.cls


public class customLookup {

    @AuraEnabled

    public static Contact getContact(String contactID){

        if(!String.isEmpty(contactID)){

            Contact con = [SELECT Id, Account.Address__c, Account.Name,       AccountId FROM Contact WHERE Id=:contactID];

            return con;

        }

        return null;

    }


    @AuraEnabled

    public static List<Account> getAccounts(){

        List<Account> accList = [SELECT Id, Address__c, Address__PostalCode__s, Address__StateCode__s, Address__City__s, Address__Street__s, Name FROM Account LIMIT 100];

        return accList;

    }

}



Add the custom lwc (custom lookup) in screen:

  • Pass ContactId from flow to LWC



  • Get the selected record ID from LWC to flow


  • Update Contact Record





4. Add the screen flow in ‘Quick Action’


  • Account (lookup field) should prepopulate with account record.





Comments