You need to sign in to do that
Don't have an account?
John Upton 8
How to a radio button select selection when saving.
I'm trying to mimic a simple hardcoded survey in a VF page (Survey Force cannot accommodate the size of the questions and/or answers I need). I wish to ask a question and use the answer to update a field on the Contact record associated with the user in question.
I'm struggling with the update - in the code below, my 'Connect__c' field on Contact is only ever set to the default value of 'xxx', not the '+5', '+4', or '+3' that I am hoping for. Can anyone suggest what I need to do differently?
Thanks
Controller:
public class TestCon1 {
String Answer = 'xxx';
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('+5','+5 choice'));
options.add(new SelectOption('+4','+4 choice'));
options.add(new SelectOption('+3','+3 choice'));
return options;
}
public TestCon1() {
Id Myid = ApexPages.currentPage().getParameters().get('id');
contact = new Contact(id=Myid, connect__c=this.Answer);
}
public String getAnswer() { return Answer; }
public void setAnswer(String Answer) { this.Answer = Answer; }
public Contact contact { get; private set; }
public PageReference save() {
try {
update(contact);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
}
PageReference NextPage = Page.SurveyQuestion2;
NextPage.getParameters().put('contactId', contact.Id);
NextPage.setRedirect(true);
return NextPage;
}
}
VF page:
<apex:page controller="MyCon1" tabStyle="Account">
<apex:form >
<apex:selectRadio value="{!Answer}" layout="pageDirection">
<apex:selectOptions value="{!items}"/>
</apex:selectRadio>
<apex:commandButton action="{!save}" value="save"/>
</apex:form>
</apex:page>
I'm struggling with the update - in the code below, my 'Connect__c' field on Contact is only ever set to the default value of 'xxx', not the '+5', '+4', or '+3' that I am hoping for. Can anyone suggest what I need to do differently?
Thanks
Controller:
public class TestCon1 {
String Answer = 'xxx';
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('+5','+5 choice'));
options.add(new SelectOption('+4','+4 choice'));
options.add(new SelectOption('+3','+3 choice'));
return options;
}
public TestCon1() {
Id Myid = ApexPages.currentPage().getParameters().get('id');
contact = new Contact(id=Myid, connect__c=this.Answer);
}
public String getAnswer() { return Answer; }
public void setAnswer(String Answer) { this.Answer = Answer; }
public Contact contact { get; private set; }
public PageReference save() {
try {
update(contact);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
}
PageReference NextPage = Page.SurveyQuestion2;
NextPage.getParameters().put('contactId', contact.Id);
NextPage.setRedirect(true);
return NextPage;
}
}
VF page:
<apex:page controller="MyCon1" tabStyle="Account">
<apex:form >
<apex:selectRadio value="{!Answer}" layout="pageDirection">
<apex:selectOptions value="{!items}"/>
</apex:selectRadio>
<apex:commandButton action="{!save}" value="save"/>
</apex:form>
</apex:page>
You need to do one of the following:
1. Update your Save method as follows
or
2. Update your Radio to use merge field so that the values are saved directly to the fields.
All Answers
You need to do one of the following:
1. Update your Save method as follows
or
2. Update your Radio to use merge field so that the values are saved directly to the fields.