vijayk3327 / LWC

1 stars 0 forks source link

Search between two dates in Lightning Web Component Using apex salesforce – LWC #83

Open vijayk3327 opened 1 year ago

vijayk3327 commented 1 year ago

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.

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.

→ Get source code live demo link:-

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

` `

Step 2:- Create Lightning Web Component : lwcSearchDate.js ` 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'
        },
    });
}

}`

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

` <?xml version="1.0" encoding="UTF-8"?>

50.0 true lightning__AppPage lightning__RecordPage lightning__HomePage ` **Step 4:- Create Apex Controller : lwcDateCmpCtrl.cls** ` 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 searchRoItem(Date dateStr1, Date dateStr2){ List agrObj = [SELECT Opportunity__c, SUM(RO_Amount__c) totalAmount FROM RO_Published__c GROUP BY Opportunity__c]; List 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; } }` **[→ Get source code live demo link:-](https://www.w3web.net/search-between-two-dates-in-lwc/)**