How to pass Date field from Salesforce Lightning Component to Apex Controller

How to pass Date field from Salesforce Lightning Component to Apex Controller?

We can’t directly pass Date field value from Lightning Component to its Apex Controller. What we need is to :

1. Capture the value of Date in a String variable in Apex function’s parameter.

2. Convert that String value to Date value.

3. Use the Date value where we want to.

Example :

DateUsage.cmp


<aura:component>

<ui:inputDate displayDatePicker="true" aura:id="expDate" format="MM-DD-YYYY"/>

</aura:component>

DateUsage.js

({

createExpDate: function(component, event,helper) {

var expirationDate = component.find("expDate").get("v.value");

var action = component.get("c.createExpDate");

action.setParams({ expDate : expirationDate });

// callback and enqueue action

}

})

DateUsageController.cls

public class DateUsageController {

@AuraEnabled

public static void createExpDate(String expDate) {

Date expirationDate = Date.valueOf(expDate);

// Now we can use expirationDate

}

}

0 Comments

Leave a Reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?