0

There are only two fields in my page, one conditionally rendering the other.

Everything works as expected (checkbox toggles the display of the date field) if I set the Boolean value to true from my constructor.

But if set to false, and I try to toggle the checkbox from UI, I get "An internal server error has occurred. An error has occurred while processing your request. The salesforce.com support team has been notified of the problem." In debug logs I get nothing worth mentioning.

Here is my code: I apologize if it is something silly I am missing, but I just could not find it out.

<apex:page controller="testctrl" docType="html-5.0">
<apex:form >
  <apex:pageBlock id="myPgBlk">
      <apex:pageBlockSection title="My Pageblocksection" columns="1">
          <apex:inputCheckbox label="Choose a custom date" value="{!chk}">
              <apex:actionSupport event="onchange" rerender="myPgBlk" action="{!NULL}"/>
          </apex:inputCheckbox>
          <apex:input label="Cust Date" value="{!custDate}" type="date" rendered="{!chk == true}"/>
      </apex:pageblockSection>
  </apex:pageBlock>
</apex:form>
</apex:page>


public class testctrl {
    public Boolean chk {get; set;}
    public Date custDate {get; set;}
    public testctrl() {
        chk = false; // works fine if initialized with true
        custDate = Date.today();
    }
}

2 Answers 2

1

The issue is, you are calling a Controller action as Null. Remove the action attribute action="{!NULL}" from the <apex:actionSupport element. It will work.

VF Page

<apex:page controller="testctrl" docType="html-5.0">
<apex:form >
  <apex:pageBlock id="myPgBlk">
      <apex:pageBlockSection title="My Pageblocksection" columns="1">
          <apex:inputCheckbox label="Choose a custom date" value="{!chk}">
              <apex:actionSupport event="onchange" rerender="myPgBlk"/>
          </apex:inputCheckbox>
          <apex:input label="Cust Date" value="{!custDate}" type="date" rendered="{!chk == true}"/>
      </apex:pageblockSection>
  </apex:pageBlock>
</apex:form>
</apex:page>

Controller

public class testctrl {
    public Boolean chk {get; set;}
    public Date custDate {get; set;}
    public testctrl() {
        chk = true; // works fine if initialized with true
        custDate = Date.today();
    }
}

Screenshots:

enter image description here

Update

It seems there are some issues with HTML 5 elements. Meanwhile, I got it working by wrapping the checkbox with an actionRegion tag.

<apex:page controller="testctrl" docType="html-5.0">
    <apex:form >
        <apex:pageBlock id="myPgBlk">
            <apex:pageBlockSection title="My Pageblocksection" columns="1">
                <apex:pageblockSectionItem >
                    <apex:outputLabel value="Choose a custom date" for="chkbox"/> 
                    <apex:actionRegion>
                        <apex:inputCheckbox id="chkbox" value="{!chk}">
                            <apex:actionSupport event="onchange" rerender="myPgBlk"/>
                        </apex:inputCheckbox>
                    </apex:actionRegion>
                </apex:pageblockSectionItem >
                <apex:pageblockSectionItem >
                    <apex:input label="Cust Date" value="{!custDate}" type="date" rendered="{!chk == true}"/>
                </apex:pageblockSectionItem >
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
8
  • Sorry that didn't work. Same error. And I thought it is usual practice to POST the page using this NULL action. I learnt to do this from The Visualforce Basic course at Linda.com. Commented Feb 5, 2018 at 11:56
  • @SFDCDev27 I have tested the code before posting, please find the screenshot attached. Maybe the issue is somewhere else.
    – Saroj Bera
    Commented Feb 5, 2018 at 12:03
  • well that is exactly what I mentioned in my original question. It does work if the Boolean is initialized to true from the constructor. Commented Feb 5, 2018 at 12:06
  • Have you tried seeing what happens if the boolean is set to false? Commented Feb 5, 2018 at 12:06
  • 1
    It is working if you wrap the apex:inputCheckbox inside and apex:actionRegion.
    – Saroj Bera
    Commented Feb 5, 2018 at 13:07
1

There seems some issue more specific to initialization of custDate in regards with apex:input type="date"

Try commenting initialization of custDate should work fine.

public class testctrl {
   public Boolean chk {get; set;}
   public Date custDate {get; set;}
   public testctrl() {
      chk = false; // works fine if initialized with true
      //custDate = Date.today();
    }
}

In case you need a default value then find the below workaround:

VF Page:

<apex:page controller="testctrl" docType="html-5.0">
   <apex:form >
   <apex:pageBlock id="myPgBlk">
      <apex:pageBlockSection title="My Pageblocksection" columns="1">
          <apex:inputCheckbox label="Choose a custom date" value="{!chk}">
              <apex:actionSupport event="onchange" rerender="myPgBlk" action="{!setDefaultDate}" />
          </apex:inputCheckbox>
          <apex:input label="Cust Date" value="{!custDate}" type="date" rendered="{!chk}" />       
      </apex:pageblockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public class testctrl {
    public Boolean chk {get; set;}
    public Date custDate {get; set;}
    public testctrl() {
       chk = false; 
    }

    //Initialize the date here on change of Boolean chk
    public void setDefaultDate() {
        if (custDate != null ) {
           return;
        }
        custDate = date.Today();
    }
}

All I have done above is on change of the boolean, I have initialized the date variable in case not initialized. Hope this will work fine for you.!

Although, there are some issues with UI experience using this tag. For more reference please check this document Date Picker for Visualforce Input tag of type DATE does not work

4
  • Wow thanks a lot that worked! Though this opens up new issues for me, as I must use an input type = date, I don't have an inputField. But at least I know what is going on. Commented Feb 5, 2018 at 13:06
  • I found a new workaround, just quoting answer. Give me a moment. Commented Feb 5, 2018 at 13:09
  • Thanks! that workaround gets over that issue of initializing the date. Now I have two answers that both work great for me.. thank you both:) However to choose one, I am choosing the answer from @Saroj that helps me overcome this issue by no change in Controller code. Commented Feb 5, 2018 at 14:11
  • Sure, that perfectly makes more sense.! Learning for me indeed. Happy Coding.! Commented Feb 6, 2018 at 6:08

You must log in to answer this question.

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