function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mona KawaleMona Kawale 

I want to show contacts of Account when search button is clicked using Apex call with imperative method. Please check below code

HTML FILE

<template>
    <lightning-card title="Contacts Of Account">
        <div class="slds-p-around_medium">
         <lightning-input
         type="text"
         value={account}
         label="Enter Account Name"
         onkeyup={textHandler}></lightning-input>
       
         <lightning-button
         label="Search"
         size="small" variant="brand"
         onclick={handleAccountSearch} ></lightning-button>
       
        <h2>Account Name :- <span><strong>{currentAccName}</strong></span></h2><br/>
        <table class="slds-table slds-table_cell-buffer slds-table_bordered"
        border="1" cellspacing="0" cellpadding="0"
        style="border-collapse:collapse">
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Account Name</th>
                </tr>
            </thead>
            <tbody>
                <template if:true={accounts}>
                <template for:each={accounts} for:item="account">
                    <tr key={account.Name}>
                        <td>{account.FirstName}</td>
                        <td>{account.LastName}</td>
                        <td>{account.Email}</td>
                        <td>{account.Phone}</td>
                        <td>{account.Account.Name}</td>
                    </tr>
                </template>
            </template>
            </tbody>
        </table>
        </div>
    </lightning-card>
</template>


JS FILE


import { LightningElement ,track,wire} from 'lwc';
import ConMethod from '@salesforce/apex/AccountController.ConMethod'
export default class FindContactOfAccount extends LightningElement {
     searchKey =''
    @track searchAccName
    @track currentAccName
    @track accounts
    textHandler(event){
        this.currentAccName=event.target.value
    }
    handleAccountSearch(){
  this.searchKey=target.currentAccName
    ConMethod({searchKey:this.searchKey})
    .then((result)=>{
     this.accounts=result;
     this.error = undefined;
   }).catch((error)=>{
    this.error = error;
    this.contacts = undefined;  
 });
}
}

Class File

public with sharing class AccountController {
    @AuraEnabled(cacheable=true)
    public static list<Account> getAccountList(){
        return[SELECT Id, Name, Type, Industry from Account where Industry!=Null LIMIT 6];
     }
     @AuraEnabled(cacheable=true)
    public static list<Account> filterAccountbyType(String type){
        return[SELECT Id, Name, Type from Account where type=: type LIMIT 6];
     }
     @AuraEnabled(cacheable=true)
    public static list<Account> findAccounts(String searchKey){
        String key='%'+ searchKey +'%';
        return[SELECT Id, Name, Type, Industry, (Select Id, Lastname from Contacts) from Account where Name like :key LIMIT 6];
     }
      @AuraEnabled(cacheable=true)
        public static list<Account> AccMethod(){
        list<Account> Acclist=[SELECT Id, Name, Type, Industry from Account];
        return Acclist;
     }
     @AuraEnabled(cacheable=true)
        public static list<Contact> ConMethod(String searchKey){
        list<Contact> ConList=[SELECT Id, FirstName,LastName,Email,Phone,  Account.Name from Contact where Account.Name=:searchKey];
        return ConList;
     }
   }

In output, when I click on search button ,contacts of accounts are not showing.

Please Answer