Oct 12, 2024
88 Views
Comments Off on How to pass Date field from Salesforce Lightning Component to Apex Controller?
0 0

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

Written by

We can’t directly pass the 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
    }
}

I hope this solution works for you to pass the Date field from Salesforce Lightning Component to Apex Controller.

Article Categories:
Blog
Priya Singh
https://thestarbiznews.com/

Expertise: Technology Trends, Startups, Digital Disruption Bio: Priya Singh is a tech-savvy millennial with a finger on the pulse of the ever-evolving digital landscape. A graduate of Stanford's Computer Science program and a former engineer at a Silicon Valley giant, Priya has a deep understanding of the technologies shaping our future. Her passion lies in demystifying complex tech jargon and exploring the disruptive potential of emerging trends, making her articles essential reading for anyone who wants to stay ahead of the curve. When not scouring the web for the latest tech tidbits, Priya enjoys traveling to off-the-beaten-path destinations and immersing herself in diverse cultures, always seeking new inspiration and insights.