Value provided is invalid for action parameter ‘fieldName’ of type ‘Date

How to pass value to date parameter in apex method from LWC JS?
How to pass value to date parameter in apex method from LWC JS?

What you will learn:

  • 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

Value provided is invalid for action parameter 'dateOfBirth' of type 'Date

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
    }
				
			

LWC to apex call

				
					dateOfBirth = new Date();
    onSaveRecord() { 
        doSomethingOnDate({ date: this.dateOfBirth })
        .then((data) => {
            // your code
        })
        .catch((error) => { 
            // return error
        });
    }
				
			

Why this error happens

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);
}
dateOfBirth = null;

handleDateChange(event) {
    // lightning-input type=date gives a YYYY-MM-DD string
    this.dateOfBirth = event.detail.value || null;
}

onSaveRecord() {
    saveDateOfBirth({ recordId: this.recordId, dateOfBirth: this.dateOfBirth })
        .then(() => { /* success */ })
        .catch((error) => { console.error(error); });
}

If you build the date yourself, use new Date().toISOString().slice(0, 10). That gives a YYYY-MM-DD string, in UTC time.

FAQ

Can I pass null?
Yes. Use null when there is no date. An empty string is not valid.

What about DateTime?
Apex DateTime accepts an ISO string, so a JavaScript date converted with toISOString() works there.

Leave a Reply