Search between two dates in Lightning Web Component Using apex salesforce – LWC | Search between two dates get the all records start date and end date using search functionallty In LWC | How to find number of days between two dates using Apex in Salesforce LWC

3,888 views

Hey guys, today in this post we are going to learn about Search between two dates get the all records start date and end date using search functionallty In LWC.

What is Global Search in Salesforce

When a user clicks into the global search bar they will see their recent items. As they start typing in the search box, they will be shown recent items that match their search criteria. If the correct record is displayed. To know more details about search function, Click Here.

Challenges in your career

🎯 If You Are Facing Any Of These 6 Challenges in your career.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

Files we used to Search between two dates in LWC →

lwcSearchDate.html LWC HTML File Template HTML file to retrieve data between two dates in LWC
lwcSearchDate.js LWC JavaScript File In the javascript file, display the search results between two dates in LWC.
lwcSearchDate.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage..
lwcDateCmpCtrl.cls Apex Class Controller It is used for get the all records start date and end date in apex method.

 

Final Output →

Search between two dates in salesforce lwc -- w3web.net
 
 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

 

  • Find the below steps ▾

 

Create Lightning Web Component HTML →

Step 1:- Create Lightning Web Component : lwcSearchDate.html

SFDX:Lightning Web Component >> New >> lwcSearchDate.html

lwcSearchDate.html [Lightning Web Component HTML]

  1.  <template>
  2. <lightning-card>
  3.     <div class="slds slds-p-around_large slds-form">
  4.         <h3 class="slds-text-heading_medium"><lightning-icon icon-name="custom:custom109" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px; margin-right:5px;"> Search Publish RO  </strong> </h3>        
  5.     <br/>
  6.     <div class="slds-wrap slds-grid">
  7.  
  8.  
  9.     <div class="slds-col slds-size_5-of-12 slds-p-horizontal--small slds-m-bottom--medium">           
  10.         <div class="slds-form-element__control ">
  11.             <lightning-input type="Date" label="From" value={oldDate} name="oldDate" onchange={handleChangeAction}></lightning-input>
  12.         </div>
  13.     </div>
  14.     <div class="slds-col slds-5-of-12 slds-p-horizontal--small slds-m-bottom--medium">           
  15.         <div class="slds-form-element__control">
  16.             <lightning-input type="Date" label="To" value={oldDate2} name="oldDate2" onchange={handleChangeAction}></lightning-input>
  17.         </div>
  18.     </div>
  19.  
  20.     <div class="slds-col slds-2-of-12 slds-m-bottom--medium" style="text-align:center; width:40px; margin:auto;">
  21.         <lightning-button type="brand" label="Search" variant="brand" size="small" onclick={searchAction}></lightning-button>
  22.         </div>
  23.  
  24.     </div> 
  25.  
  26.  
  27.  
  28.     <hr>
  29.     <table class="slds-table slds-table_bordered slds-table_col-bordered slds-table_fixed-layout slds-table_resizable-cols" border="1" cellpadding="5" cellspacing="5" style="border-collapse:collapse; border:1px #ddd solid;"> 
  30.  
  31.         <thead>
  32.             <tr>
  33.                 <th style="padding:10px;">RO Amount</th>
  34.                 <th style="padding:10px;">RO Date</th>
  35.                 <th style="padding:10px;">Opportunity Id</th>
  36.                 <th style="padding:10px;">Opportunity Name</th>
  37.                 <th style="padding:10px;">Account Id</th>
  38.             </tr>
  39.         </thead>
  40.         <tbody>  
  41.     <template for:each={roDateList} for:item="item">
  42.         <tr key={item.Id}>
  43.             <td>{item.RO_Amount__c}</td>
  44.             <td>{item.RO_Date__c}</td>
  45.             <td>{item.Opportunity__c}</td>
  46.             <td>{item.Opportunity__r.Name}</td>
  47.             <td><a name={item.Opportunity__r.AccountId} onclick={actionToPublishedNav}>{item.Opportunity__r.AccountId}</a></td>
  48.         </tr>
  49.  
  50.         </template>
  51.  
  52.     </tbody>
  53.  
  54.     </table>
  55. </div> 
  56.  
  57.  
  58. </lightning-card>
  59. </template>

 

Create Lightning Web Component JavaScript →

Step 2:- Create Lightning Web Component : lwcSearchDate.js

SFDX:Lightning Web Component >> New >> lwcSearchDate.js

lwcSearchDate.js [LWC JavaScript File]

  1.   import { LightningElement,track } from 'lwc';
  2. import searchRoItem from '@salesforce/apex/lwcDateCmpCtrl.searchRoItem';
  3. import { NavigationMixin } from 'lightning/navigation'; 
  4. export default class LwcSearchDate extends NavigationMixin(LightningElement){
  5.     @track firstName;
  6.     @track lastName;
  7.     @track oldDate; 
  8.     @track oldDate2; 
  9.     @track newDate; 
  10.     @track newDate2;
  11.     @track dateRecoreId;
  12.     @track errorMsg;
  13.     @track roDateList;
  14.     @track recordId;
  15.  
  16.     handleChangeAction(event){
  17.  
  18.  
  19.         if(event.target.name == 'oldDate'){
  20.             this.oldDate = event.target.value;  
  21.  
  22.             window.console.log('oldDate ##' + this.oldDate);
  23.         }
  24.  
  25.         if(event.target.name == 'oldDate2'){
  26.             this.oldDate2 = event.target.value;  
  27.             window.console.log('oldDate2 ##' + this.oldDate2);
  28.         }
  29.  
  30.     }
  31.  
  32.  
  33.  
  34.     searchAction(){
  35.  
  36.         searchRoItem({dateStr1:this.oldDate,dateStr2:this.oldDate2})
  37.         .then(result=>{
  38.  
  39.             this.dateRecoreId = result.Id;
  40.             this.roDateList=result;
  41.              //window.console.log('dateRecoreId##Vijay2 ' + this.dateRecoreId);       
  42.              window.console.log('roDateList ' + JSON.stringify(this.roDateList));       
  43.                 })
  44.         .catch(error =>{
  45.            this.errorMsg=error.message;
  46.            window.console.log(this.error);
  47.         });
  48.      }
  49.  
  50.  
  51.      actionToPublishedNav(event) {
  52.         this.recordId=event.target.name;
  53.         window.console.log('logAAA ' + this.recordId + 'LogBBB ' + event.target.name);
  54.         this[NavigationMixin.Navigate]({
  55.             type: 'standard__recordPage',
  56.             attributes: {
  57.                 recordId: this.recordId,
  58.                 objectApiName: 'Account',
  59.                 actionName: 'view'
  60.             },
  61.         });
  62.     }
  63. }

 

Create Lightning Web Component Meta XML →

Step 3:- Create Lightning Web Component : lwcSearchDate.js-meta.xml

SFDX:Lightning Web Component >> New >> lwcSearchDate.js-meta.xml

lwcSearchDate.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>50.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.     </targets>
  10. </LightningComponentBundle>

 

Create Apex Class Controller →

Step 4:- Create Apex Controller : lwcDateCmpCtrl.cls

SFDX:Create Apex Class >> New >> lwcDateCmpCtrl.cls

lwcDateCmpCtrl.cls [Apex Class]

  1.     public WITH sharing class lwcDateCmpCtrl {
  2.   @AuraEnabled
  3.   public static RO_Published__c submitROAction(INTEGER roAmount,DATE roDate){
  4.       RO_Published__c pubObj = NEW RO_Published__c();
  5.       pubObj.RO_Amount__c=roAmount;
  6.       pubObj.RO_Date__c=roDate;
  7.  
  8.     INSERT pubObj;
  9.     system.debug('pubObj@@@ ' + pubObj);
  10.     RETURN pubObj;
  11.   }
  12.  
  13.   @AuraEnabled
  14.   public static List<RO_Published__c> searchRoItem(DATE dateStr1, DATE dateStr2){
  15.  
  16.     List<AggregateResult> agrObj = [SELECT Opportunity__c, SUM(RO_Amount__c) totalAmount FROM RO_Published__c GROUP BY Opportunity__c];
  17.     List<RO_Published__c> roPublisherObj = [SELECT Id, Name, RO_Amount__c, RO_Date__c, Opportunity__c, Opportunity__r.Name, Opportunity__r.AccountId FROM RO_Published__c WHERE RO_Date__c >:dateStr1 AND RO_Date__c <:dateStr2];
  18.      system.debug('roPublisherObj ' + roPublisherObj);
  19.  
  20.       RETURN roPublisherObj;
  21.   }
  22.  
  23. }

 

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How do I find the number of months between two dates in Salesforce?

To find the number of months between two dates, subtract the year of the earlier date from the year of the later date and multiply the difference by 12. Next, subtract the month of the earlier date from the month of the later date, and add that difference to the value of the first set of operations.

How do you find the value between two dates?

You can use a formula based on the LOOKUP function and the DATE function to achieve the result of doing vlookup operation to lookup a date and return value.

How do I find the difference between two dates in the same column?

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.

Related Topics | You May Also Like

 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment