0

I have try to create custom list view on Account but I have faced some issue while I am selecting the combo Box value How to retrieved apex class data ?

Please help me to solve this issue.....

and my component look like this.. How to fix the picklist in Right side ?

Component

html :-

   <template>
     <lightning-card class="myCard" variant="Narrow" icon-name="standard:account" 
    label="Accounts">
    <lightning-combobox name="progress" value={filter} options={options}
        onchange={comboBoxHandleChange} slot="actions" class="slds-float_left 
   block_left"></lightning-combobox>

        <lightning-button-group slot="actions">
            <lightning-button label="New"></lightning-button>
            <lightning-button label="Import"></lightning-button>
            <lightning-button label="Printable View"></lightning-button>
        </lightning-button-group>

        <div class="slds-p-around_medium">
            <p class="slds-border_bottom">{dataSize} item : Filtered by {filter}</p>

            <lightning-datatable columns={columns} 
                                     data={accData} 
                                     hide-checkbox-column="true"
                                     key-field="id"></lightning-datatable>
            
        </div>

</lightning-card>

JS:-

   import { LightningElement, wire } from 'lwc';
   import GetAccountData from '@salesforce/apex/FSLAccountClass.allAccountMethod';
   // datatable columns
 const cols = [
        {label: 'Account Name',fieldName: 'Name'}, 
        {label: 'Account Site',fieldName: 'Site'},
        {label: 'Phone',fieldName: 'Phone',type: 'phone'}, 
        {label: 'Rating',fieldName: 'Rating'},  
    ];

 export default class FslListViewCompo extends LightningElement {

columns = cols;
filter
accData;
dataSize;

@wire(GetAccountData)
accData({error, data}){
    if(data){
        this.accData = data;
        this.dataSize = data.length;
        console.log('Account Data Received by Wire method '+ JSON.stringify(this.accData));
        this.error = undefined;
    } else if(error){
        this.error = error;
    }
}


get options(){
    return [
        { label: 'All Accounts', value: 'all Accounts' },
        { label: 'Recently Account', value: 'Recently viewed' },
        { label: 'This Month Accounts', value: 'month' },
        { label: 'Half Yearly Accounts', value: 'half_year' },
        { label: 'This Year Accounts', value: 'year' },
    ]
}

comboBoxHandleChange(event){
    this.filter = event.target.value;
}
}

Apex:-

public with sharing class FSLAccountClass {
   public FSLAccountClass() {
      System.debug('Hi');
   }

@AuraEnabled(cacheable=true)
public static List<Account> recentlyViwedAccMethod(){
    try {
    
        List<Account> accList = [SELECT Id, Name, LastViewedDate, Site, Phone, Rating FROM account WHERE LastViewedDate != null ORDER BY LastViewedDate DESC ];
    return accList;
        
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
        
    }
    
}

@AuraEnabled(cacheable=true)
public static List<Account> thisMonthAccMethod(){
    try {
        
        List<Account> thisMoAccList = [SELECT Id, Name, CreatedDate, Site, Phone, Rating FROM Account WHERE CreatedDate = THIS_MONTH WITH SECURITY_ENFORCED];
        return thisMoAccList;

    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
        
    }
}

@AuraEnabled(cacheable=true)
public static List<Account> halfYearAccMethod(){
    try {
        
        List<Account> halfYearAccList = [SELECT Id, Name, CreatedDate, Site, Phone, Rating FROM Account WHERE CreatedDate = THIS_QUARTER WITH SECURITY_ENFORCED];
        return halfYearAccList;

    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
        
    }
}

@AuraEnabled(cacheable=true)
Public static List<Account> thisYearAccMethod(){
    try {
        
        List<Account> thisYearAccList = [SELECT Id, Name, CreatedDate, Site, Phone, Rating FROM Account WHERE CreatedDate = THIS_YEAR WITH SECURITY_ENFORCED];
        return thisYearAccList;
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
        
    }
}

@AuraEnabled(cacheable=true)
public static List<Account> allAccountMethod(){
    try {
        
        List<Account> allAccList = [select id, Name, Site, Phone, Rating from Account WITH SECURITY_ENFORCED];
        return allAccList;

    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
        
    }
    }
  }

1 Answer 1

0

You need to do an imperative apex call in the comboBoxHandleChange. https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_call_imperative

I observed that you are not filtering the data in SOQL based on the combo box value. You need to pass it.

You need to mention which method you want to use to get the filtered data.

For example - I'm considering the filter value as all Accounts.

import GetAccountData from '@salesforce/apex/FSLAccountClass.allAccountMethod';

comboBoxHandleChange(event){
    this.filter = event.target.value;
    if(this.filter ==='all Accounts'){
      GetAccountData()
            .then((result) => {
                this.accs = result;
                this.error = undefined;
            })
            .catch((error) => {
                this.error = error;
                this.contacts = undefined;
            });
      }
   }

@AuraEnabled(cacheable=true)
public static List<Account> allAccountMethod(){
      try {
        
        List<Account> allAccList = [select id, Name, Site, Phone, Rating from Account WITH SECURITY_ENFORCED];
        return allAccList;

    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
        
    }
    
}

so you just need to add multiple conditions based on the Combobox value.

2
  • Can u tell me about how to filtering the data in SOQL based on the combo box value in LWC ?
    – Crazy Man
    Jan 17, 2023 at 9:11
  • I think you are still learning, so I added a brief example. Jan 17, 2023 at 10:04

You must log in to answer this question.

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