+ Start a Discussion
SudhaaaaSudhaaaa 

need to filter records using search button.

Hi,
From this below code i am getting list of accounts with search bar. when i search for  particular account, the below code is not working.
what should i add  to filter records from list of records which i got. please help me out from this issue.

Thank you :)


Component:
<aura:component  controller="AccountRecord">
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 
    <aura:attribute name="namevalue" type="string"/>
    <aura:attribute name="Account" type="Account"/>
    
    <lightning:input label='' value="{!v.namevalue}" /> 
    <br></br>
   <div><lightning:button label="Search" onclick="{!c.handleClick}"/><br/></div>
    
   <!-- <lightning:button variant="destructive" label="search" onclick="{! c.handleClick }"/> -->
    
    <table class="slds-table slds-table_cell-buffer slds-table_bordered ">
        <thead>
            <tr class="slds-line-height_reset">
                <th scope="col">
                    <div class="slds-truncate">ACCOUNT ID</div>
                </th> 
                <th scope="col">
                    <div class="slds-truncate">ACCOUNT NAME</div>
                </th> 
                <th scope="col">
                    <div class="slds-truncate">STATUS</div>
                </th> 
                
            </tr>
             </thead>
        
         <aura:iteration items="{!v.Account}" var="account">
            <tbody>
                
                <tr class="slds-hint-parent">
                    <td scope="row" data-label="Account Id">
                        <div class="slds-truncate"> {!account.Id}</div>
                    </td>
                    <td scope="row" data-label="Account Name">
                        <div class="slds-truncate"> {!account.Name}</div>
                    </td>
                    <td scope="row" data-label="Account Status">
                        <div class="slds-truncate"> {!account.Status__c}</div>
                    </td>     
                </tr>
                
            </tbody>
              </aura:iteration>       
       
    </table>
    
    
</aura:component>

Controller
({
        handleClick : function(component, event, helper) {
        var acc1=component.get("v.namevalue");
         var action = component.get("c.accountInfo");
        action.setParams({
            acc: acc1
        });
        action.setCallback(this, function(response){
            var similarProperties = response.getReturnValue();
            component.set("v.Account", similarProperties);
        });
        $A.enqueueAction(action);
                }, 
    doInit: function(component, event, helper){
    var action = component.get("c.accountInfo1");
        action.setCallback(this, function(response){
            debugger;
            var similarProperties1 = response.getReturnValue();
            console.log(similarProperties1);
            //var myJSON = JSON.stringify(similarProperties1);
            //alert(myJSON);
            component.set("v.Account", similarProperties1);
        });
        $A.enqueueAction(action);
    }
})

Apex class:
public class AccountRecord {
    @AuraEnabled
    public static String accountInfo(String acc){
        Account ac=[select id,name,status__c from account where name=:acc limit 1];
        String acc1=ac.id+' '+ac.name+' '+ac.status__c;
        return acc1;
       
    }
     @AuraEnabled
    public static List<Account> accountInfo1(){
          list <Account> Ac1=[select id,name,status__c from account];
        return Ac1;
       
    } 
}




 
Best Answer chosen by Sudhaaaa
Danish HodaDanish Hoda
Hi Reshma,
Please refer below code:
@AuraEnabled
    public static List<Account> accountInfo(String acc){
        Account ac=[select id,name,status__c from account where name=:acc limit 1];
        String acc1=ac.id+' '+ac.name+' '+ac.status__c;
        List<Account> accList = new List<Account>();
        acclist.add(ac);
        return acclist;
       
    }

 

All Answers

Danish HodaDanish Hoda
Hi Reshma,
The logic is fine and when you will put console log, you will find the Account record with the namevalue you are searching for.

The issue is with the component - UI which is not getting re-rendered when you are trying to search with the namevalue.
SudhaaaaSudhaaaa
@Danish Hoda I am not able to get you. can you update my code?
Danish HodaDanish Hoda
Hi Reshma,
Please refer below code:
@AuraEnabled
    public static List<Account> accountInfo(String acc){
        Account ac=[select id,name,status__c from account where name=:acc limit 1];
        String acc1=ac.id+' '+ac.name+' '+ac.status__c;
        List<Account> accList = new List<Account>();
        acclist.add(ac);
        return acclist;
       
    }

 
This was selected as the best answer
Vikash GoyalVikash Goyal
Hi Reshma,

Main problem here is with return type of 'accountInfo' method. In your lightning component, you are displaying account records using 'Account' attribute which is type of Account i.e. it can be object or list<object>. 

So in your first case i.e. in case of doinit you are returing List<Account> from method 'accountInfo1' so it is working fine.
But in other case where you are searching for particular account, 'accountInfo' method is called which has return type as 'String' thats why lightning component is not able to find any value for  {!account.Id}, {!account.Name} and {!account.Status__c}.

To fix this you have to update return type of 'accountInfo' method i.e. your 'AccountRecord' class will look like this :
 
public class AccountRecord {
    @AuraEnabled
    public static List<Account> accountInfo(String acc){
        List<Account> accList = new List<Account>([SELECT Id, Name, Status__c from account where name=:acc       limit 1]);
        return acclist;
    }
    
    @AuraEnabled
    public static List<Account> accountInfo1(){
        list <Account> Ac1=[select id,name,status__c from account];
        return Ac1;
    } 
}

Please mark this as answer if it works for you.

Thanks
SudhaaaaSudhaaaa
@Vikash Goyal Thank you so much 
SudhaaaaSudhaaaa
how to write validation rule for "when status is changed to active, need to create record otherwise no need" ??

IF( ISPICKVAL( Status__c , "Active") , value_if_true, value_if_false) 
Danish HodaDanish Hoda
Hi Reshma,
You need to use:
IF( AND(ISPICKVAL( Status__c , "Active"), ISCHANGED(Status__c)) , value_if_true, value_if_false)