I have this method getFarmData being called from LWC

        public static List<RecordWrapper> getFarmData(String listView, Integer Count) {
            if (listView== 'OWNER_FARMS') {
                return getFarms();
if (listView== 'ALL_FARMS') {
            return getAllFarms();
        if (listView== 'VERIFIER_VISITS') {
            return getVerifierVisits();
            return null;

    //this method is to get the list of farms owned by running user.
    private static List<RecordWrapper> getFarms() {
        String user = UserInfo.getUserId();
        String query = 'SELECT Id, Name, Farm_Owner__c, Verifier_Visits__c, CreatedDate FROM Farm__c' +
            ' WHERE Farm_Owner__c= :user' + 
            ' ORDER BY CreatedDate DESC LIMIT 100';
        List<Farm__c> farmData =  Database.query(query);
        List<RecordWrapper> results = new List<RecordWrapper>();
        for (Farm__c f: farmData ) {
            results.add(new RecordWrapper(f));
        return results;

I also have a date range selection in the same lwc, by selecting the from & to dates, user should see the data for that particular range. enter image description here

String query = 'SELECT Id, Name, Farm_Owner__c, Verifier_Visits__c, CreatedDate FROM Farm__c' +
            ' WHERE Farm_Owner__c= :user' AND (CreatedDate >+ :fromDate AND  CreatedDate >+ :toDate + 
            ' ORDER BY CreatedDate DESC LIMIT 100';

Can someone help me on how to make this happen on click of "Set Range" button??

Thank you

Update (LWC Code Snippet)

multiple listviews can be selected:

<lightning-combobox style="width: fit-content;" name="listview" value={listValue} options={listOptions} onchange={handleViewChange} ></lightning-combobox>

custom component with sorting and filtering:

 <c-reusable-grid fields-list={fieldsList} grid-data={gridData.data} id-field-name="Id" records-per-page="20"
                        show-select-checkbox="false" >


@track listValue = 'ALL_FARMS';
    @track fromDate = null;
    @track toDate = null;
    @track fieldsList = [
                { name: "Farm_Owner", title: "Farm Owner", type: "text", filtering: true, sorting: true},
                { name: "Verifier_Visits__c", title: "Visits", type: "text", filtering: true, sorting: true},
                { name: "CreatedDate", title: "Created Date", type: "date", filtering: true, sorting: true}

        @wire(getFarmData, { listView: '$listValue', Count: '$refreshCount' }) gridData;

            get listOptions(){
                return [
                    {label: 'All Farms', value: 'ALL_FARMS'},
                    {label: 'My Farms', value: 'MY_FARMS'},
                    {label: 'Verifier Visits', value: 'VERIFIER_VISITS'}
    handleViewChange(event) {
            this.listValue = event.detail.value;

There's no reason for dynamic queries here; Salesforce is smart enough to ignore criteria that doesn't make sense. You can simply use the following instead:

public static RecordWrapper[] getFarmData(DateTime startDateTime, DateTime endDateTime) {
    RecordWrapper[] results = new RecordWrapper[0];
    for(Farm__c record: [
        SELECT Name, Farm_Owner__c, Verifier_Visits__c, CreatedDate
        FROM Farm__c
        WHERE Farm_Owner__c = :UserInfo.getUserId() AND
            CreatedDate >= :startDateTime AND
            CreatedDate <= :endDateTime
        ORDER BY CreatedDate DESC LIMIT 100
        ]) {
        results.add(new RecordWrapper(record));
    return results;

If you only plan on returning values on a certain "list view", don't call the server unnecessarily.

Pass in the start and end date values; if the values are null, they will be ignored in the query.

I included LIMIT 100, but is that really necessary? Does a user have more than 50,000 farms to deal with? Also, is the "wrapper" really necessary? It seems like a lot of overhead for no real reason; you can directly transform the results in your wire method or just use the field names as-is. Consider simplifying your code design.

| improve this answer | |
  • Hey @sfdcfox, do you prefer @wire or using imperative apex? I guess with wire we sacrifice some control over speed. What do you think? – Atlas Can Dec 13 '19 at 6:11
  • 1
    @AtlasCan I prefer @wire when possible; it centralizes loading data to a single method, even if I decided to manipulate reactive variables from multiple handlers. That said, imperative methods are useful when you need to control the order of method calls or to limit the number of callbacks for performance reasons. The lack of control is generally easily worth the ease of using wire. – sfdcfox Dec 13 '19 at 6:17
  • @sfdcfox Thank you for the response. I updated the question with lwc code. We have a custom grid component which accepts data in a certain format fieldsList (above) which is why i used a wrapper. Yes, there will be multiple listOptions to select a listView. I will remove the LIMIT, i was testing data in QueryEditor and copied the query as-is. – SunnyG Dec 13 '19 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.