+ Start a Discussion
Sabahat InayatSabahat Inayat 

How to pass Datepicker input value from LWC input to custom field of the object

<template>
<div class="slds-col"> 
            
            <label class="slds-form-element__label slds-no-flex">Posting Date</label>
            <lightning-input type="date" 
                        name="Postingdate" 
                        value={PostingDate}
                        onchange={PostingDateChange}
            >
            </lightning-input>  
         
        </div>     
</template>

CR.html 
import { LightningElement,api ,track} from 'lwc';

import getInvoiceData from '@salesforce/apex/CustomerRefundLwcController.getInvoiceData';
import getBankAccounts from '@salesforce/apex/CustomerRefundLwcController.getBankAccounts';
import processCustomerRefund from '@salesforce/apex/CustomerRefundLwcController.processCustomerRefund';

export default class CustomerRefund extends LightningElement {
    @api recordId; /*Sales Credit Id*/
    @track invoiceData;
    @track invoiceLinesData;
    @api headerText;
    @api recordProcessing;
    @api bankAccounts = new Array();
    @track selectedBankId;
    @track allocateToSalesCredit;
    @track referenceDetail;
    @track bShowModal = false;
    @track PostingDate;
    /*Toast Event Variables*/
    @api isError = false;
    @api iswarning = false;
    @api issuccess = false;
    @api ToastMessage;


 PostingDateChange(event)
    {
        this.PostingDate = event.target.value;
        this.invoiceData.PostingDate = this.PostingDate;
    }

processRefundHandler() {
        this.recordProcessing = true;
        //console.log('this.formData----'+JSON.stringify(this.invoiceLinesData));
        console.log('this.formData----'+JSON.stringify(this.invoiceData));
        processCustomerRefund({ lstInvoiceLines : this.invoiceLinesData, bankId : this.selectedBankId, allocateToSalesCredit : this.allocateToSalesCredit, reference : this.referenceDetail, postingdate : this.PostingDate })
        .then(result => {
            var responseMessage = result;
            this.recordProcessing = false;
            
            this.ToastMessage = responseMessage;
            if(responseMessage == "Success"){
                //this.issuccess = true;
                this.isError = false;
                this.ToastMessage = "Your Bank Customer Refund has been created";
                this.bShowModal = true;
            }else{
                this.isError = true;
            }
        })
        .catch(error => {
            this.isError = true;
            this.ToastMessage = error ;
            console.log(error);
        });
    }

CR.js
public with sharing class CustomerRefundLwcController {
    public CustomerRefundLwcController() {

    }
 @AuraEnabled
    public static string processCustomerRefund(List<ScLineWrapper> lstInvoiceLines,string bankId,boolean allocateToSalesCredit,string reference){
        system.debug('start update');
        string successMessage = 'Success';
        string responseMessage = 'Success';
        Savepoint spTran = database.setSavePoint();
        try{
            List<Sales_Invoice_Line_Item__c> lstLinesToUpdate = new List<Sales_Invoice_Line_Item__c>();
            decimal amount=0;
            Id taxRate;
            Id salesinvoiceid;

            boolean isValid = true;
            boolean allocateToInvoice = allocateToSalesCredit;
            map<id,Sales_Invoice_Line_Item__c> mapOfSelectedLines = new map<id,Sales_Invoice_Line_Item__c>();

            for(ScLineWrapper lineWrapper : lstInvoiceLines){
                system.debug('isselect>> ' + lineWrapper.isSelected);
                if(lineWrapper.isSelected){
                    salesinvoiceid = lineWrapper.InvoiceLine.Sales_Invoice__c;
                    decimal lineAmount = lineWrapper.InvoiceLine.Foreign_Gross__c;
                    if(lineAmount != null && lineAmount > 0){
                        amount += lineAmount;
                        mapOfSelectedLines.put(lineWrapper.InvoiceLine.id,lineWrapper.InvoiceLine);
                    }

                    if(lineWrapper.InvoiceLine.Tax_Rate__c != null){
                        taxRate = lineWrapper.InvoiceLine.Tax_Rate__c;
                    }
                }
            }

            if(salesinvoiceid == null){
                isValid = false;
                responseMessage = 'Please select at least one sales invoice line';
            }else if(bankId == null){
                isValid = false;
                responseMessage = 'Please select bank account';
            }else if(amount <= 0){
                isValid = false;
                responseMessage = 'Refund amount should be greater than zero';
            }

            system.debug('updatelst>> ' + lstLinesToUpdate);
            //update lstLinesToUpdate;
            if(isValid){
                Sales_Invoice__c siInfo = [select id,Status__c,Account__c,Name from Sales_Invoice__c where id=: salesinvoiceid];
                BankCustomerRefundService objCustomerRefundService = new BankCustomerRefundService();
                BankCustomerRefundService.BankCustomerRefundWrapper objRefundWrapper = new BankCustomerRefundService.BankCustomerRefundWrapper();
                Id accountId = siInfo.Account__c;
                objRefundWrapper.AccountId = accountId;
                objRefundWrapper.PostingDate = Date.Today()+(-216);
                objRefundWrapper.BankAccountId = bankId;
                objRefundWrapper.TaxRate = taxRate;
                objRefundWrapper.Amount = amount;
                objRefundWrapper.Reference = reference;
                BankCustomerRefundService.Response objResponse = objCustomerRefundService.CreateBankCustomerRefund(objRefundWrapper);
                responseMessage = objResponse.ResponseMessage;
    
                if(allocateToInvoice && responseMessage == successMessage){
                    Bank_Payment__c objBankPayment = objResponse.BankPayments.get(accountId);
                    List<Ledger__c> listofLedgers = [Select id,name,Foreign_Gross_Total__c,Type__c
                                                    FROM Ledger__c
                                                    Where Customer_Supplier_Account_Name__c =: accountId AND Show_On_Transaction__c=1
                                                    AND Paid__c = 'N' AND Is_Deleted__c = false
                                                    AND (Sales_Invoice_Line_Item__c IN: mapOfSelectedLines.keyset() OR Bank_Payment__c =: objBankPayment.id) ];
                    
                    if(!listofLedgers.isEmpty()){
                        BankAllocateCreditsAndPaymentsService objAllocateCreditsAndPaymentService = new BankAllocateCreditsAndPaymentsService();
                        BankAllocateCreditsAndPaymentsService.BankAllocateCreditsAndPaymentsWrapper objAllocateCreditWrapper = new BankAllocateCreditsAndPaymentsService.BankAllocateCreditsAndPaymentsWrapper();
                        objAllocateCreditWrapper.AccountId = accountId;
                        objAllocateCreditWrapper.PostingDate = Date.Today()+(-216);
                        List<BankAllocateCreditsAndPaymentsService.BankAllocateCreditsAndPaymentsLineWrapper> BankAllocateCreditsAndPaymentsLines = new List<BankAllocateCreditsAndPaymentsService.BankAllocateCreditsAndPaymentsLineWrapper>();
                        for(Ledger__c ledger : listofLedgers){
                            BankAllocateCreditsAndPaymentsService.BankAllocateCreditsAndPaymentsLineWrapper objPaymentLine = new BankAllocateCreditsAndPaymentsService.BankAllocateCreditsAndPaymentsLineWrapper();
                            objPaymentLine.LedgerName = ledger.Name;
                            objPaymentLine.Amount = ledger.Foreign_Gross_Total__c;
                            BankAllocateCreditsAndPaymentsLines.add(objPaymentLine);
                        }

                        objAllocateCreditWrapper.BankAllocateLines = BankAllocateCreditsAndPaymentsLines;
                        BankAllocateCreditsAndPaymentsService.Response objAllocateResponse = objAllocateCreditsAndPaymentService.BankAllocateCustomerBalance(objAllocateCreditWrapper);
                        responseMessage = objAllocateResponse.ResponseMessage;
                        system.debug('Response>>' + objAllocateResponse.ResponseMessage);
                    }                                                    
                }
                
            }
        }catch(Exception ex){
            responseMessage = ex.getStackTraceString();
        }
        
        if(responseMessage != successMessage){
            Database.rollback(spTran);
        }
        system.debug('Response>>' + responseMessage);
        return responseMessage;
        
    }

CRLWCController.cls

I want to use LWC datepicker input to pass date value in CRLWC.cls in PostingDate variable. 

Can anyone help please
Saravana Bharathi 1Saravana Bharathi 1
Hi Sabahat,

Pass date value as string from Lwc Javascript file to apex class and parse the date value in apex to get it as date value and you can assign it to date field in apex.

Your HTML file in lwc looks fine.

In Javascript

PostingDateChange(event) {
this.PostingDate = event.target.value;
processCustomerRefund({ lstInvoiceLines : this.invoiceLinesData, bankId : this.selectedBankId, allocateToSalesCredit : this.allocateToSalesCredit, reference : this.referenceDetail, postingdate : this.PostingDate })
}


In Apex class

@AuraEnabled public static string processCustomerRefund(List<ScLineWrapper> lstInvoiceLines,string bankId,boolean allocateToSalesCredit,string reference,String postingdate){
Date postingdatevalue = Date.valueOf(postingdate);//By this, you can get the date value in apex to assign to field value.
}

Try this and let me know, if it solves and mark it as resolved.

Thanks
Saravana