I'm having trouble sending Date values from the client with remoted methods. If my RemoteAction method takes a plain Date value, everything works fine with the suggestion of using "toUTCString" which I found here. Unfortunately this technique does not appear to work for Date values that are parts of SObjects (either specific ones such as Contact or the anonymous "SObject" type).
Test case controller:
public with sharing class DateRemotingTest {
@RemoteAction
public static void acceptDate(Date d) {
System.debug('got date:' + String.valueOf(d)); // works
}
@RemoteAction
public static void acceptContact(Contact c) {
insert c; // fails
System.debug('got Contact with date:' + String.valueOf(c.BirthDate));
delete c;
}
}
Test case page:
<apex:page controller="DateRemotingTest">
<script>
DateRemotingTest.acceptDate((new Date()).toUTCString(),
function(result, event) {
if (event.status) {
console.log('plain date worked');
} else {
console.log('plain date had some kind of error: ' + event.message);
}});
DateRemotingTest.acceptContact({LastName: 'Xi',
FirstName: 'JinPing',
Birthdate: (new Date(1953,5,1)).toUTCString()
},
function(result, event) {
if (event.status) {
console.log('plain date worked');
} else {
console.log('plain date had some kind of error: ' + event.message);
}
});
</script>
</apex:page>
Loading the page results in the error
Insert failed. First exception on row 0; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, Birthdate: value not of required type: Mon, 01 Jun 1953 07:00:00 GMT
I have tried numerous variations on the format, but the server isn't happy with any of them. What is the correct way of doing this?
Edit: I've created a much fancier test case - almost a console for experimenting with Date serialization style. controller and page.