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
Kunal Purohit 4Kunal Purohit 4 

How to display record based on selected dates?

Hi, i am having Object named Research_Paper__c . How to dispay records depending on selected dates.. For example,after clicking show button, i want to display name of Research Papers whose publication date is between 1/8/2020 and 14/8/2020.. How to do it in Lightning?
AnudeepAnudeep (Salesforce Developers) 
Hi Kunal - I recommend looking at the example code in this answer

Let me know if it helps
Abhishek BansalAbhishek Bansal
Hi Kunal,

Please follow the below steps:
  1. Create a lightning componet with three attributes:
    • Start Date
    • End Date
    • List to hold records between start date and end date
  2. Add two input type of Date in component and one button called Submit
  3. Now on click of this Submit button, call your apex method that will take these two dates as input and return the records between these dates via SOQL.
  4. Store this result in the list
  5. Display the list in component.
Let me know if you need any other help on this.

Thanks,
Abhishek Bansal.
yogesh watileyogesh watile
Aura Component
<aura:component controller="Accountobjcontroller" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
 <aura:attribute name="accList" type="Account[]"/>
 <aura:attribute name="columns" type="List"/>
 <aura:attribute name="fromDate" type="date"/>
 <aura:attribute name="toDate" type="date"/>
    
    
 <aura:handler name="init" value="{! this }" action="{! c.doinit }"/>

 <lightning:input name="date1" label="From Date" type="date" value="{!v.fromDate}"/>
 <lightning:input name="date2" label="To Date" type="date" value="{!v.toDate}"/>
 <lightning:button variant="brand" label="Get Accounts" iconName="utility:search" onclick="{!c.getAcconts}"/>
 <div style="height: 300px">
 <lightning:datatable
 keyField="id"
 data="{! v.accList }"
 columns="{! v.columns }"
 hideCheckboxColumn="true"/>
 </div>
     
</aura:component>
 
JS Controller 
({
    doinit : function(cmp, event, helper){
 
 cmp.set('v.columns', [
 {label: 'Account name', fieldName: 'Name', type: 'text'},
 {label: 'Rating', fieldName: 'Rating', type: 'text'},
 {label: 'Type', fieldName: 'Type', type: 'text'},
 {label: 'SLA', fieldName: 'SLA__c', type: 'text'}, 
 {label: 'Created Date', fieldName: 'CreatedDate', type: 'date'}
 ]);
 },
 getAcconts : function(cmp, event, helper) {
console.log('In Controller');
 helper.getAccountHelper(cmp);
 }
})

helper.js

({
 getAccountHelper : function(cmp) {
 
 var action=cmp.get('c.getAccountList');
 
 var frDate=cmp.get('v.fromDate');
 var trDate=cmp.get('v.toDate');
 
 action.setParams({fDate : frDate, tDate : trDate});
 
     action.setCallback(this, function(response){
 
         var state=response.getState();
 
 if(state=='SUCCESS'){
 
 cmp.set('v.accList', response.getReturnValue());
 }
 else{
 console.log('No records found');
 }
 });
 $A.enqueueAction(action);
 }
})


Apex Controller
public class Accountobjcontroller{

    @auraEnabled
 public static List<Account> getAccountList(date fDate, date tDate){
 system.debug('From Date: '+fDate);
 system.debug('To Date: '+tDate);
 List<Account> accList=new List<Account>();
 accList=[select id, name, rating, type, SLA__c, createdDate from Account where 
createdDate>=:fDate and createdDate<=:tDate];
 if(!accList.isEmpty()){
 return accList;
 }
 else{
 return null;
 }        
    }
}