0

I have search input box on searching the account names in the input box it should display the list of account names but i am unable to see the result i inspected my code but don't see any issues. Can you help me what is the issue.

public with sharing class AccountController {
  
    @AuraEnabled(cacheable=true)
    public static List<Account> findAccounts(String searchKey){
        String key = '%' + searchKey + '%';
        return [SELECT Id, Name, Type, Industry FROM Account WHERE Name LIKE :key LIMIT 5];
    }
}


Apexdemo.html

<template>
    <lightning-card title="Apex Wire To Property Demo">
        <div class="slds-p-around_medium">
            <lightning-input type="search" 
            label="Search Account" value={searchKey} 
            onchange={searchHandler}>

            </lightning-input>
        </div>
        <template if:true={accounts}>
            <template for:each={accounts} for:item="account">
                <div class="slds-box slds-box_xx-small" key={account.Id}>
                    <p>Name -{account.name}</p>
                    <p>Type -{account.Type}</p>
                    <p>Industry -{account.Industry}</p>
                </div>
            </template>
        </template>
    </lightning-card>
</template>

Apexdemo.js file :

import { LightningElement } from 'lwc';
import findAccounts from '@salesforce/apex/AccountController.findAccounts'
export default class ApexImperativeDemo extends LightningElement {
    searchKey=''
    accounts
    timer
    searchHandler(event){
        window.clearTimeout(this.timer)
        this.searchKey = event.target.value
        this.timer = setTimeout(()=>{
            this.callApex()
        }, 1000)
    }

    callApex(){
        findAccounts({searchKey:this.searchKey})
        .then(result=>{
            this.accounts = result
        }).catch(error=>{
            console.error(error)
        })
    }
}

Output of the result

4
  • try to console your result in js and list<Account> in your apex controller. Also instead of writing just accounts use @track accounts=[]; in your js file Commented Jun 12, 2021 at 14:22
  • @ShagunSharma I have used console.log in the below statment but don't see any values passed after searching in input filed under developer console console tab and used the track decorator .then(result=>{ this.accounts = result console.log(result)
    – Saikumar
    Commented Jun 12, 2021 at 14:49
  • I have run the SOQL from query editor could see that the results are being fetched correctly
    – Saikumar
    Commented Jun 12, 2021 at 14:52
  • I was able to see under object property and cleared the browser cache and redeployed the code it worked Object apexImperativeDemo.js:4 (2) [{…}, {…}] 0: {Id: "0015g00000FtsZDAAZ", Name: "Test for Account"} 1: {Id: "0015g00000FtsquAAB", Name: "Test"}
    – Saikumar
    Commented Jun 12, 2021 at 15:04

1 Answer 1

1

I tested your code.It seems working for me .Only issue I found that it was not displaying account Name(as you are using account.name change it with account.Name).However it was displaying Type and Industry after I provided suitable searchKey.

Code

<div class="slds-box slds-box_xx-small" key={account.Id}>
                <p>Name -{account.Name}</p>
                <p>Type -{account.Type}</p>
                <p>Industry -{account.Industry}</p>
            </div>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .