Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Last active October 13, 2015 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefftrull/4247933 to your computer and use it in GitHub Desktop.
Save jefftrull/4247933 to your computer and use it in GitHub Desktop.
Reproducing Date serialization issue - VF page
<apex:page controller="DateIssueRepro">
<!-- Simple VF page to demonstrate issue with setting Date fields of SObjects via Remoting -->
<!-- This is case 08471144 -->
<!-- Author: Jeff Trull <jetrull@sbcglobal.net> 2012-12-09 -->
<apex:form >
<table>
<tr>
<th>Date source</th>
<th>New value</th>
</tr>
<tr>
<td><apex:outputLabel value="SObject field" for="sobjdate"/></td>
<td><apex:inputText id="sobjnewvalue"/></td>
<td><input type="button" value="Submit"
onclick="setDate('{!$Component.sobjnewvalue}','{!$Component.sermethod}')"/> </td>
</tr>
<tr>
<td><apex:outputLabel value="Date" for="regulardate"/></td>
<td><apex:inputText id="regularnewvalue"/></td>
<td><input type="button" value="Submit"
onclick="setDate('{!$Component.regularnewvalue}','{!$Component.sermethod}')"/></td>
</tr>
</table>
<apex:outputLabel value="Serialization Method:" for="sermethod" style="font-weight: bold"/>
<apex:selectRadio id="sermethod" value="{!selectedSer}">
<apex:selectOption itemValue="none" itemLabel="None"/>
<apex:selectOption itemValue="epoch" itemLabel="Seconds since Epoch"/>
<apex:selectOption itemValue="date" itemLabel="Javascript Date"/>
<apex:selectOption itemValue="utc" itemLabel="UTC String"/>
</apex:selectRadio>
</apex:form>
<script>
function setDate(inputid, methodid) {
var datestr = document.getElementById(inputid).value;
// find desired method from radio buttons
var methodels = document.getElementsByName(methodid);
var method;
for (var i = 0; i < methodels.length; i++) {
if (methodels[i].checked) {
method = methodels[i].value;
}
}
console.log('using method "' + method + '" to process "' + datestr + '"');
var serializedDate;
if (method == 'none') {
// just send it directly
serializedDate = datestr;
} else if (method == 'epoch') {
serializedDate = new Date(datestr).getTime(); // works for Date field inside sObject (as of Spring 13)
} else if (method == 'date') {
serializedDate = new Date(datestr);
} else if (method == 'utc') {
serializedDate = new Date(datestr).toUTCString(); // works for plain Date values
} else {
console.log('unknown serialization method:', method);
return;
}
console.log('serialized data is:', serializedDate);
// launch to appropriate remote method
if (endsWith(inputid, 'regularnewvalue')) {
DateIssueRepro.updateDate(serializedDate,
function(response, e) {
if (!e.status) {
console.log('call failed with status', e.message);
} else {
console.log('call succeeded');
}});
}
if (endsWith(inputid, 'sobjnewvalue')) {
DateIssueRepro.updateContact({Birthdate: serializedDate},
function(response, e) {
if (!e.status) {
console.log('call failed with status', e.message);
} else {
console.log('call succeeded');
}});
}
}
// utility function. Thanks, Stack Overflow:
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
</script>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment