+ Start a Discussion
John Upton 8John 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>
Best Answer chosen by John Upton 8
Rajiv Penagonda 12Rajiv Penagonda 12
John, Salesforce VF page input elements need to be linked to the fields on the record. Without that the values will not be committed on insert/update.
 
You need to do one of the following:
1. Update your Save method as follows
public PageReference save() {
	try {
		contact.connect__c = this.Answer;
		update(contact);
	}
	catch(System.DMLException e) {
		ApexPages.addMessages(e);
	}
}

or
2. Update your Radio to use merge field so that the values are saved directly to the fields.
<apex:form >
	<apex:selectRadio value="{!contact.connect__c}" layout="pageDirection">
	<apex:selectOptions value="{!items}"/>
	</apex:selectRadio>
	<apex:commandButton action="{!save}" value="save"/>
</apex:form>

 

All Answers

Rajiv Penagonda 12Rajiv Penagonda 12
John, Salesforce VF page input elements need to be linked to the fields on the record. Without that the values will not be committed on insert/update.
 
You need to do one of the following:
1. Update your Save method as follows
public PageReference save() {
	try {
		contact.connect__c = this.Answer;
		update(contact);
	}
	catch(System.DMLException e) {
		ApexPages.addMessages(e);
	}
}

or
2. Update your Radio to use merge field so that the values are saved directly to the fields.
<apex:form >
	<apex:selectRadio value="{!contact.connect__c}" layout="pageDirection">
	<apex:selectOptions value="{!items}"/>
	</apex:selectRadio>
	<apex:commandButton action="{!save}" value="save"/>
</apex:form>

 
This was selected as the best answer
John Upton 8John Upton 8
Hi Rajiv - I did the latter, it worked as hoped, thanks!