Skip to main content

I am facing issues when I create a custom Lightning Search Component using my VS Code . It is not deploying it to my org . 

Error Msg :- Unable To Find Apex Action Method Referenced as   AccountSearchCls.getAccounts() ;

 

here's my code 

 

controller :- 

 

public with sharing class AccountController {  

  

    @AuraEnabled( cacheable = true )  

    public static List< Account > getAccounts( String searchKey ) {  

        String strKey = '%' + searchKey + '%';  

        return [ SELECT Id, Name, Industry,Website FROM Account WHERE Name LIKE: strKey];  

          

    }  

      

 

JS File :-

 

import { LightningElement,track  } from 'lwc';

import fetchAccounts from '@salesforce/apex/AccountSearchCls.fetchAccounts'; 

const columns = [  

    { label: 'Name', fieldName: 'Name' },  

    { label: 'Industry', fieldName: 'Industry' },

    { label: 'Website', fieldName: 'Website' },    

];  

  

export default class AccountSearchUsingLWC extends LightningElement {

  

     accounts;  

     error;

     columns = columns;  

  

     handleKeyChange( event ) {  

          

        const searchKey = event.target.value;  

  

        if ( searchKey ) {  

  

            fetchAccounts( { searchKey } )    

            .then(result => {    

                this.accounts = result; 

            })  

            .catch(error => {  

  

                this.error = error;  

  

            });  

  

        } else  

        this.accounts = undefined;  

  

    }  

 

XML File :-

 

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

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

    <apiVersion>48.0</apiVersion>

    <isExposed>true</isExposed>

     <targets>

        <target>lightning__AppPage</target>

        <target>lightning__RecordPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle> 

 

HTML File ":-

 

<template>  

      

    <lightning-card title = "Search Accounts using lwc">  

  

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

  

            <lightning-input type = "search" onchange = {handleKeyChange} class = "slds-m-bottom_small" label = "Search" >  

            </lightning-input>  

  

            <template if:true = {accounts}>  

                  

                <div>  

  

                    <lightning-datatable key-field="Id"  

                                         data={accounts}  

                                         columns={columns}  

                                         hide-checkbox-column="true"  

                                         show-row-number-column="true">  

                    </lightning-datatable>  

  

                </div>                   

      

            </template> 

 

Can anyone help me out here ? I am not able to understand what's the issue  here .

 

#Lightning Web Components  #Lightning Component  #Sales Cloud  #Nonprofit  #Trailhead  #New Releases

      

            <template if:true={error}>  

                No records found!

 

            </template>  

 

        </div>  

  

    </lightning-card>  

  

</template> 

2 respostas
  1. 6 de out. de 2021, 06:13

    Hi Yash Muley,

    You are mistaking one line in JS file.

     

    Incorrect line in JS file i.e. second line.

    import fetchAccounts from '@salesforce/apex/AccountSearchCls.fetchAccounts';

     

    Correct line in that file according to the above controller will be:

    import fetchAccounts from '@salesforce/apex/AccountController.getAccounts';

     

                                         OR(you can also write this)

     

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

     

    I hope. These line will help you.

     

    Thank you!

    Regards,

    Suraj Tripathi 

0/9000