You need to sign in to do that
Don't have an account?
suji 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;
}
}
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;
}
}
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Javascript is case sensitive.
The below line is having the small letter h.
helper.Searchhelper(component,event,helper); ==> 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
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!