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.
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 →
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]
<template>
<lightning-card>
<div class="slds slds-p-around_large slds-form">
<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>
<br/>
<div class="slds-wrap slds-grid">
<div class="slds-col slds-size_5-of-12 slds-p-horizontal--small slds-m-bottom--medium">
<div class="slds-form-element__control ">
<lightning-input type="Date" label="From" value={oldDate} name="oldDate" onchange={handleChangeAction}></lightning-input>
</div>
</div>
<div class="slds-col slds-5-of-12 slds-p-horizontal--small slds-m-bottom--medium">
<div class="slds-form-element__control">
<lightning-input type="Date" label="To" value={oldDate2} name="oldDate2" onchange={handleChangeAction}></lightning-input>
</div>
</div>
<div class="slds-col slds-2-of-12 slds-m-bottom--medium" style="text-align:center; width:40px; margin:auto;">
<lightning-button type="brand" label="Search" variant="brand" size="small" onclick={searchAction}></lightning-button>
</div>
</div>
<hr>
<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;">
<thead>
<tr>
<th style="padding:10px;">RO Amount</th>
<th style="padding:10px;">RO Date</th>
<th style="padding:10px;">Opportunity Id</th>
<th style="padding:10px;">Opportunity Name</th>
<th style="padding:10px;">Account Id</th>
</tr>
</thead>
<tbody>
<template for:each={roDateList} for:item="item">
<tr key={item.Id}>
<td>{item.RO_Amount__c}</td>
<td>{item.RO_Date__c}</td>
<td>{item.Opportunity__c}</td>
<td>{item.Opportunity__r.Name}</td>
<td><a name={item.Opportunity__r.AccountId} onclick={actionToPublishedNav}>{item.Opportunity__r.AccountId}</a></td>
</tr>
</template>
</tbody>
</table>
</div>
</lightning-card>
</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]
import { LightningElement,track } from 'lwc';
import searchRoItem from '@salesforce/apex/lwcDateCmpCtrl.searchRoItem';
import { NavigationMixin } from 'lightning/navigation';
export default class LwcSearchDate extends NavigationMixin(LightningElement){
@track firstName;
@track lastName;
@track oldDate;
@track oldDate2;
@track newDate;
@track newDate2;
@track dateRecoreId;
@track errorMsg;
@track roDateList;
@track recordId;
handleChangeAction(event){
if(event.target.name == 'oldDate'){
this.oldDate = event.target.value;
window.console.log('oldDate ##' + this.oldDate);
}
if(event.target.name == 'oldDate2'){
this.oldDate2 = event.target.value;
window.console.log('oldDate2 ##' + this.oldDate2);
}
}
searchAction(){
searchRoItem({dateStr1:this.oldDate,dateStr2:this.oldDate2})
.then(result=>{
this.dateRecoreId = result.Id;
this.roDateList=result;
//window.console.log('dateRecoreId##Vijay2 ' + this.dateRecoreId);
window.console.log('roDateList ' + JSON.stringify(this.roDateList));
})
.catch(error =>{
this.errorMsg=error.message;
window.console.log(this.error);
});
}
actionToPublishedNav(event) {
this.recordId=event.target.name;
window.console.log('logAAA ' + this.recordId + 'LogBBB ' + event.target.name);
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
objectApiName: 'Account',
actionName: 'view'
},
});
}
}
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]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>50.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Create Apex Class Controller →
Step 4:- Create Apex Controller : lwcDateCmpCtrl.cls
SFDX:Create Apex Class >> New >> lwcDateCmpCtrl.cls
lwcDateCmpCtrl.cls [Apex Class]
public WITH sharing class lwcDateCmpCtrl {
@AuraEnabled
public static RO_Published__c submitROAction(INTEGER roAmount,DATE roDate){
RO_Published__c pubObj = NEW RO_Published__c();
pubObj.RO_Amount__c=roAmount;
pubObj.RO_Date__c=roDate;
INSERT pubObj;
system.debug('pubObj@@@ ' + pubObj);
RETURN pubObj;
}
@AuraEnabled
public static List<RO_Published__c> searchRoItem(DATE dateStr1, DATE dateStr2){
List<AggregateResult> agrObj = [SELECT Opportunity__c, SUM(RO_Amount__c) totalAmount FROM RO_Published__c GROUP BY Opportunity__c];
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];
system.debug('roPublisherObj ' + roPublisherObj);
RETURN roPublisherObj;
}
}
Further post that would you like to learn in Salesforce
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 |