1

For a number of apex:inputfields in Visualforce, I'm suppressing the Salesforce date picker and using the browser based one. I'm experiencing odd behaviour when attempting to do so on the Task ActivityDate field:

Example

Markup:

    <apex:page controller="TestController" docType="html-5.0">
        <apex:form id="theForm">
            <apex:inputField value="{!t.ActivityDate}" showDatePicker="false" type="date"/>
        </apex:form>
   </apex:page>

Controller:

public with sharing class TestController {
    public Task t{get; set;}
}

When trying to compile the VF code, I get the following error:

Expected input type 'datetime-local', got 'date' for Date data type

I don't get this error for other Date fields in the database, which have all worked fine. I've also checked the DisplayType for the field and is definitely is being returned as a Date field like all the others.

Out of interest, I gave the compiler what it wanted and changed the type to "Datetime-local" and it saved fine. However, running the page produced an Internal Server error.

Has anybody else run across this issue and resolved it? Or is the answer to create a controller variable to proxy for the ActivityDate field?

9
  • Can you post a more complete MVR? One complete, minimal page that can be copied into any org to reproduce the issue will get you an answer more quickly.
    – Adrian Larson
    Commented Nov 23, 2016 at 14:26
  • @AdrianLarson - Certainly. I've edited to update.
    – Bigears
    Commented Nov 23, 2016 at 15:08
  • I'm intentionally suppressing the datepicker in favour of the browser specific options in this use case.
    – Bigears
    Commented Nov 23, 2016 at 15:10
  • Ah, I follow. Have you considered javascript alternatives such as jquery's datepicker?
    – Adrian Larson
    Commented Nov 23, 2016 at 15:11
  • 1
    The example you've used to update the question is subtly different to my original. When only using Standard Controller, the error doesn't appear. It only shows up when using the Controller, rather than standard controller.
    – Bigears
    Commented Nov 23, 2016 at 15:20

2 Answers 2

0

One solution is to change the input type dynamically through JavaScript. I tried with the below approach and it is working in Chrome. As the input field is html date type it should work in mobile devices too.

<apex:page controller="TestController1" docType="html-5.0" >
    <script type="text/javascript">
         function stringToDate(_date,_format,_delimiter){
            var formatLowerCase=_format.toLowerCase();
            var formatItems=formatLowerCase.split(_delimiter);
            var dateItems=_date.split(_delimiter);
            var monthIndex=formatItems.indexOf("mm");
            var dayIndex=formatItems.indexOf("dd");
            var yearIndex=formatItems.indexOf("yyyy");
            var month=parseInt(dateItems[monthIndex]);
            month-=1;
            var formatedDate = new Date(dateItems[yearIndex],month,dateItems[dayIndex]);
            return formatedDate;
         }
    </script>    
    <apex:form id="theForm">
        <apex:inputField showDatePicker="false" value="{!t.ActivityDate}" id="activityDate" style="width:110px"  />            
    </apex:form>
    <script type="text/javascript">      
        var elem = document.getElementById('{!$Component.theForm.activityDate}');
        var mydate = stringToDate(elem.value,"dd/mm/yyyy","/").toISOString().substring(0, 10);
        elem.value = '';
        elem.type = 'date';
        elem.value = mydate;
    </script>    
</apex:page>
1
  • Unfortunately, I found this produced an error on save. I also found a simpler approach that worked for me in the near-term. I will update an answer here accordingly.
    – Bigears
    Commented Nov 29, 2016 at 17:00
0

In the end, I made use of a "dummy" property in the controller to represent the date and an field:

<apex:input value="{!activity.aDate}" id="ActivityDateField" styleClass="form-control" type="date"/>

I then used the getters and setters of the property to ensure they were aligned with the ActivityDate on the Task record. Not the most elegant solution, but worked for my purposes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .