0

Markup

<apex:page controller="HomeController">
  <apex:form >
    Day <apex:inputText value="{!strday}"/><br />
    Month <apex:inputText value="{!strmonth}"/><br />
    Year <apex:inputText value="{!stryear}"/>
    <apex:commandButton action="{!openPageTwo}" title="Open Page2" value="Open Page 2"/>
  </apex:form>
</apex:page>

Page 1 Controller

public without sharing class HomeController {
    public String strday {get;set;}
    public String strmonth {get;set;}
    public String stryear {get;set;}

    public PageReference openPageTwo(){
      system.debug('*************** day = ' + strday);
      system.debug('*************** month = ' + strmonth);
      system.debug('*************** year = ' + stryear);  
      PageReference newPage = page.page2;
        newPage.getParameters().put('day', strday);
        newPage.getParameters().put('month', strmonth);
        newPage.getParameters().put('year', stryear);
        newPage.setRedirect(true);
        return newPage;
    }

Page 2 Controller

public class page2{
   String day;
   String month;
   String year;
   Datetime specificDate;Datetime specificDate;
   public page2(){
     day = apexpages.currentPage().getParameters().get('day');
     month = apexpages.currentPage().getParameters().get('month');
     year = apexpages.currentPage().getParameters().get('year');

     specificDate = DateTime.parse(month + '/' + day + '/' + year + ' 0 09:00 AM');
  }
}

The page opens as desired but the parameter values are null, if I check the log the values are also null. I can't explain why the inputtext get;set; values are not being saved and passed to the pagereference.

Where am I going wrong?

0

set the values in the constructor

public class page2{
String day;
String month;
String year;
Datetime specificDate;
public page2(){
    day = apexpages.currentPage().getParameters().get('day');
    month = apexpages.currentPage().getParameters().get('month');
    year = apexpages.currentPage().getParameters().get('year');
    specificDate = DateTime.parse(month + '/' + day + '/' + year +' '+ '09:00 AM');
    system.debug('@@@'+specificDate);
    }
}
|improve this answer|||||
  • I actually am doing that, I just copied my code into the post wrong. It doesn't explain why the debug's in page 1 controller are null too? Thanks for correct though. – n34_panda Apr 26 '17 at 17:10
  • weird. I just copy pasted your code for page1 and made above changes in page2. it worked fine – night crawler Apr 26 '17 at 17:19
  • Is there any security or browser issues to consider (I am using Firefox and a system admin account)...note no sharing in the class (adding it didn't work...) – n34_panda Apr 26 '17 at 17:23
  • can't think of anything. i just tried in firefox and it worked fine. what's the api version? – night crawler Apr 26 '17 at 17:28
  • I found it - I was going over the code (which is a bit longer than I quoted) and stupidly the commandbutton was in a different apex form tag...feel free to emphasis the importance of this in your answer and i'll mark it. Do you know if you can (or should) pass date's in parameters (rather than parsing it back to date on the new page? – n34_panda Apr 26 '17 at 21:10
0

The missing ingredient, which I failed to show in the question was the that the apex:commandbutton element must be within the same apex:form tags.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.