0

I have a vf page which have two radio button Yes or No. I want selected value in constructor after clicking on submit buton. here is code.

 <apex:page controller="CCO_ScheduleACallController">
<apex:form >
<apex:pageBlock >
<apex:outputLabel style="Bold">Are you able to launch your software or service?</apex:outputLabel>
<apex:selectRadio value="{!radiovalue}">
          <apex:selectOption itemLabel="Yes" itemValue="1"></apex:selectOption>
          <apex:selectOption itemLabel="No" itemValue="2"></apex:selectOption>
      </apex:selectRadio>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!Submit}" value="Submit"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Since in the exisitng contrroler most of the logic is implemented in constructor. And now i have to add condition IF(selected value=='Yes') { //code } else { // code }

| improve this question | | | | |
  • You should put your logic in submit merhod, as contructor is getting called during initialization. – Santanu Boral May 22 '18 at 10:19
  • 1
    The Submit button's action isn't executed until after the constructor. You should refactor your controller to move logic out of the constructor. – David Reed May 22 '18 at 10:42
1

No this is not possible. You can't get user selected value in constructor.First constructor run then User get result on UI. You can set any value from Constructor which will display to user.

 public class CCO_ScheduleACallController{
    public CCO_ScheduleACallController(){

         radiovalue = '1'; // user will get Yes pre selected
    }
| improve this answer | | | | |
1

Constructor will call at the time of page loading.But note that we cant do to any dml operation on constructor. for example if submit method contains any dml operation you will get error(so you can handle it in action method!!)

I believe you are setting default value. That default value you can access in server side and also you can change that value in constructor( please check selected value)

  public class CCO_ScheduleACallController{
  public boolean selectvalue {get;set;}
    public CCO_ScheduleACallController(){
     selectvalue=false;

    }
| improve this answer | | | | |
  • 1
    We will never get user selected value from current page in constructor. – Tushar Sharma May 22 '18 at 10:43
  • 2
    This code is not valid. submit() returns a PageReference, and you have an undefined variable in your submit() method. – David Reed May 22 '18 at 10:46
  • ya. I agreed but i believe he is passing default value (thats not user selected value) – Annappa PH May 22 '18 at 10:49

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.