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
suji srinivasansuji srinivasan 

Hi, I need to fetch the account list by searchkeyword

How to achieve this with helper. 
component:
<aura:component controller="AccountSearch" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <!--button-->
    <aura:attribute name="searchkeyword" type="string" description="used to store user search"/>
    <aura:attribute name="TotalnoofRecords" type="integer" default="0" description="used to display the  no of records"/>
    <aura:attribute name="searchResult" type="List" description="used to store and display result"/>
    <aura:attribute name="message" type="boolean" description="used to  display message"/>
    <aura:attribute name="AccList" type="Account[]" />
    <aura:attribute name="columns" type="List"/>
    <!--aura:handler name="init" value="{!this}" action="{!c.fetchAccount}" /-->
    <lightning:button variant="brand" label="search" iconName="utility:search" onclick="{!c.Search}"/>
    <lightning:input value ="{!v.searchkeyword}"
                keyField="True"
                placeholder="search accounts.."
                label="AccountName"/>
      
    </aura:component>
controller:
({
     Search:function(component,event,helper) {
         helper.Searchhelper(component,event,helper);
     }
     })
Helper:
({
    SearchHelper : function(component,event) {
        
       component.set('v.columns', [
          {label:'AccountName', fieldName:'Name', type: 'text'},
          {label:'Type', fieldName:'Type', type: 'text'},
          {label:'Industry', fieldName:'Industry', type: 'text'},
          {label:'Phone', fieldName:'Phone', type: 'Phone'},
          {label:'Fax', fieldName:'Fax', type: 'text'}]);

           
        //Step1: Get the Apex method that should be invoked
        
         var action = component.get("c.fetchAccount");
         
       
         //step2: set the parameters
         action.setParams({ 
           'searchword':component.get("v.searchkeyword")
           
         });
        //Step4: handle the response from apex class method/Server side method
        action.setCallback(this, function(response){
            var storeResponse = response.getReturnValue();
            if (storeResponse.length==0){
                component.set("v.message",true);
            }
            else{
                component.set("v.message",false);
            }

           component.set("v.TotalnoofRecords",storeResponse.length);
            component.set("v.searchResult",storeResponse);
           
        });
        //Step3: Enqueue action/ApexMethod to be executed 
        $A.enqueueAction(action);
     

       
    }
})

Apex:
public class AccountSearch {
@AuraEnabled
    public static List<Account> fetchAccount(string searchword){
        string Searchkey = '%'+searchword+'%';
        List<Account> AccList = [select id,Name,Type,Industry,Phone,Fax from Account where Name=:Searchkey LIMIT 500];
        return AccList;
}
  
}
Best Answer chosen by suji srinivasan
CharuDuttCharuDutt
Hii Suji
Try Below Code
public class searchAccountController {
@AuraEnabled
 public static List < account > fetchAccount(String searchKeyWord) {
  String searchKey = searchKeyWord + '%';
  List < Account > returnList = new List < Account > ();
  List < Account > lstOfAccount = [select id, Name, Type, Industry, Phone, Fax from account where Name LIKE: searchKey limit 1];

  for (Account acc: lstOfAccount) {
   returnList.add(acc);
  }
  return returnList;
 }
}




<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="LightningDataTableController">
<!-- attributes -->
<aura:attribute name="data" type="Map"/>
<aura:attribute name="filteredData" type="Map"/>
<aura:attribute name="columns" type="List"/>
<!-- handlers-->
<aura:handler name="init" value="{!this }" action="{!c.init }"/>
<span>
<lightning:input onchange="{!c.searchTable}" type="search" label="Searh" variant="label-hidden" placeholder="Enter search term" aura:id="SearchBox"/>
</span>
<br/>
<lightning:datatable
columns="{!v.columns}"
data="{!v.filteredData}"
keyField="id"
/>
</aura:component>



Controller

({
findAccount : function(component, event, helper) {
         component.set('v.columns', [
          {label:'AccountName', fieldName:'Name', type: 'text'},
          {label:'Type', fieldName:'Type', type: 'text'},
          {label:'Industry', fieldName:'Industry', type: 'text'},
          {label:'Phone', fieldName:'Phone', type: 'Phone'},
          {label:'Fax', fieldName:'Fax', type: 'text'}]);
        var action=component.get('c.fetchAccount');
        action.setParams({
            searchKeyWord : component.get("v.keywordHolder")
            
        });   // setParams is optional, since we are expecting parameter in apex controller method we are passing the user entered value in apex controller method.
        
        action.setCallback(this,function(response){          // Getting the response back
            var state=response.getState();

            var response1=response.getReturnValue();
            if(state==="SUCCESS")
            {
                component.set("v.accountList",response1);
            }
            
        });
        $A.enqueueAction(action);

}
})
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

Maharajan CMaharajan C
Hi Suji,

Javascript is case sensitive.

The below line is having the small letter h.


helper.Searchhelper(component,event,helper);   ==>   helper.SearchHelper(component,event,helper);

 
({
    Search:function(component,event,helper) {
        console.log(' ++++ ');
        helper.SearchHelper(component,event,helper);
    }
})

Samples:

https://sfdcmonkey.com/2016/12/15/search-accounts-lightning-component/

https://gmkreddysfdc.blogspot.com/2019/08/simple-account-search-lightning.html


Thanks,
Maharajan.C

 
CharuDuttCharuDutt
Hii Suji
Try Below Code
public class searchAccountController {
@AuraEnabled
 public static List < account > fetchAccount(String searchKeyWord) {
  String searchKey = searchKeyWord + '%';
  List < Account > returnList = new List < Account > ();
  List < Account > lstOfAccount = [select id, Name, Type, Industry, Phone, Fax from account where Name LIKE: searchKey limit 1];

  for (Account acc: lstOfAccount) {
   returnList.add(acc);
  }
  return returnList;
 }
}




<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="LightningDataTableController">
<!-- attributes -->
<aura:attribute name="data" type="Map"/>
<aura:attribute name="filteredData" type="Map"/>
<aura:attribute name="columns" type="List"/>
<!-- handlers-->
<aura:handler name="init" value="{!this }" action="{!c.init }"/>
<span>
<lightning:input onchange="{!c.searchTable}" type="search" label="Searh" variant="label-hidden" placeholder="Enter search term" aura:id="SearchBox"/>
</span>
<br/>
<lightning:datatable
columns="{!v.columns}"
data="{!v.filteredData}"
keyField="id"
/>
</aura:component>



Controller

({
findAccount : function(component, event, helper) {
         component.set('v.columns', [
          {label:'AccountName', fieldName:'Name', type: 'text'},
          {label:'Type', fieldName:'Type', type: 'text'},
          {label:'Industry', fieldName:'Industry', type: 'text'},
          {label:'Phone', fieldName:'Phone', type: 'Phone'},
          {label:'Fax', fieldName:'Fax', type: 'text'}]);
        var action=component.get('c.fetchAccount');
        action.setParams({
            searchKeyWord : component.get("v.keywordHolder")
            
        });   // setParams is optional, since we are expecting parameter in apex controller method we are passing the user entered value in apex controller method.
        
        action.setCallback(this,function(response){          // Getting the response back
            var state=response.getState();

            var response1=response.getReturnValue();
            if(state==="SUCCESS")
            {
                component.set("v.accountList",response1);
            }
            
        });
        $A.enqueueAction(action);

}
})
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
suji srinivasansuji srinivasan
Thank you Maharajan and charu. I had included your suggestions and got output successfully.