0

I've been trying to get a selected value from a radio button (apex:selectradio, i'm styling it with the 5-stars like Uber) in a Visualforce Page and save it to a field called Quality_Content__c from a custom object called Feedback__c. For that, I created a controller called CaseFeedbackController. Basically, I want my Feedback__c.Quality_Content__c field to be updated with my rating value from the radiobutton.

What exactly is off here?

Thank you!

VF

<apex:page StandardController="Feedback__c" extensions="CaseFeedbackController"
standardStylesheets="true" lightningStylesheets="true" docType="html-5.0" cache="true">
<apex:outputPanel styleClass="star-rating">
    <apex:form id="form">
    <apex:pageBlock id="block">

    <apex:outputPanel styleClass="star-rating">
    <apex:form id="form">
    <apex:pageBlock id="block">
    <div style="text-align: center">{!if(isnull(rating),'No value selected',rating&' stars')}
            <br/>
            <apex:selectRadio value="{!rating}" layout="lineDirection">
                <apex:actionSupport event="onclick" reRender="form" />
                <apex:selectOption itemValue="1"></apex:selectOption>
                <apex:selectOption itemValue="2"></apex:selectOption>
                <apex:selectOption itemValue="3"></apex:selectOption>
                <apex:selectOption itemValue="4"></apex:selectOption>
                <apex:selectOption itemValue="5"></apex:selectOption>
            </apex:selectRadio>
        </div>
        </apex:pageBlock>
    </apex:form>
</apex:outputPanel>
  <apex:form >
        <apex:pageBlock >
        <apex:inputField required="true" value="{!Feedback__c.Quality_Content__c}"/> // this is just to check the field's value after saving
        <apex:pageBlockButtons location="both">
            <apex:commandButton action="{!save}" oncomplete="customSave();" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>
        </apex:pageBlock>
  </apex:form>
</apex:page>

CONTROLLER

public class CaseFeedbackController {

    public Feedback__c feed;

    public CaseFeedbackController(ApexPages.StandardController controller) {
        Feedback__c feed = (Feedback__c)controller.getRecord();
        this.feed = [select id, Quality_Content__c from Feedback__c where Id = :feed.ID];
     }

    String rating = '';

    public String getRating() {
        return rating;
    }
    public void setRating(String rating) {
        this.rating = rating;
    }

    public void customSave() {

        try {
            feed.Quality_Content__c = this.rating;
            update feed;
            // Feedback__c.Quality_Content__c = this.rating;
            // update(Feedback__c);
        }

        catch(System.DMLException e) {
            ApexPages.addMessages(e);
        }
    }

}
0

You disassociated the feed variable in your controller from the page's version of the record (from StandardController). You'll want to remove this line:

this.feed = [select id, Quality_Content__c from Feedback__c where Id = :feed.ID];

Further, you don't actually need to use your rating variable or getter/setter; just bind to the field directly:

<apex:selectRadio value="{!Feedback__c.Quality_Content__c}" layout="lineDirection">

You can reference this anywhere:

<div style="text-align: center">{!if(isblank(Feedback__c.Quality_Content__c),'No value selected',Feedback__c.Quality_Content__c&' stars')}
2
  • Thank you for that, I just did it and it helped. Now my radio is being updated when I change my inputfield and save <apex:inputField required="true" value="{!Feedback__c.Quality_Content__c}"/> , but I still can't do it the other way around (change the radio selection and update my field). I am sure I'm still missing something... – Juan Farinha Jun 25 '19 at 13:13
  • i found my mistake. I just added an apex:actionSupport event="onchange" action="{!Save}"> to the radios div and it worked. Thank you for yourl help. – Juan Farinha Jun 25 '19 at 13:42

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.