how to resolve : Value provided is invalid for action parameter ‘fieldName’ of type ‘Date’ in LWC call to Apex
getting error : Value provided is invalid for action parameter ‘dateOfBirth’ of type ‘Date while creating record in apex
How to pass value to date parameter in apex method from LWC JS?
Error in console
We were getting “value provided is invalid for action parameter ‘dateOfBirth’ of type ‘Date” this error when we are trying to pass date type of value from lighting component to the apex method where apex is used to performing some data modification operations.
How to resolve ??
// Make sure you pass null if date value not provided
dateOfBirth = ''; // error
dateOfBirth; // error
dateOfBirth = null; // correct
Apex method with Date parameter
@AuraEnabled(cacheable = true)
private void doSomethingOnDate(Date date) {
// performing DML on date
}
An Apex Date parameter expects a text value in the format YYYY-MM-DD. If you pass a JavaScript Date object, an empty string or an undefined value, Salesforce cannot convert it and returns the message Value provided is invalid for action parameter of type Date.
Mistakes in the example above
dateOfBirth = new Date() creates a JavaScript Date object. This is itself a cause of the error. Send a date string instead.
An @AuraEnabled method must be public static. A private void method will not compile as an Aura method.
A method that performs DML must not use cacheable=true.
Avoid naming the parameter date, which is also a type name.
Correct Apex and JavaScript
@AuraEnabled
public static void saveDateOfBirth(Id recordId, Date dateOfBirth) {
update new Contact(Id = recordId, Birthdate = dateOfBirth);
}