function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SamirSSamirS 

I have have designed a vf page with two radio button. I have created a field of type checkbox. I want to store boolean value against the radio button. Please help me.

Here is my VF page:
<apex:page standardStylesheets="false" controller="RadioButtonTestController" showHeader="false" sidebar="false">
    <apex:form >
    	<div align="center">
          
        	<apex:selectRadio value="{!Country}" > 
               India <apex:selectOption itemValue="true"></apex:selectOption>&nbsp;&nbsp;
    		  China &nbsp;&nbsp;<apex:selectOption itemValue="false"></apex:selectOption>

    			<apex:actionSupport event="onchange" 
                        action="{!checkSelectedValue}" 
                        reRender="none"/>
        		</apex:selectRadio>
            
            <apex:commandButton value="Show country" action="{!showCountry}"/>
    	</div>
    </apex:form>
</apex:page>

Here is my Controller
public class RadioButtonTestController 
{
    public Boolean Country{get; set;}
    public void checkSelectedValue()
    {
        
        system.debug('Selected value is: '+ Country);
    }
    
    public void showCountry()
    {
        String caseId = ApexPages.currentPage().getParameters().get('CaseID');
        list<Storing_Location__c> str = [Select Country__c from Storing_Location__c where CaseNumID__c=: caseId];
        Storing_Location__c obj = new Storing_Location__c();
        if(str.size()>0)
        {
            obj.Id = str[0].Id;
        }
        
        obj.Country__c = Country;
        system.debug('Selected value is:***********************:::'+Country);
        
    }
}

 
Pramodh KumarPramodh Kumar
@Sam Sadhukhan

In your vf page the code should be. In your code itemValue was set to true or false, in order to get the value you have to give the itemValue. 

And also, in your controller the make the country string instead of boolean


public String Country{get; set;}
<apex:selectRadio value="{!Country}" > 
               India <apex:selectOption itemValue="India"></apex:selectOption>&nbsp;&nbsp;
    		  China &nbsp;&nbsp;<apex:selectOption itemValue="China"></apex:selectOption>]
        		</apex:selectRadio>

Let me know if you still have any issues.

Thanks
Pramodh.
SamirSSamirS
Thank you Pramodh for your response. In your code you are storing string value such as "India " or "China". But my reuirement is , when I check the radio for 'India' then a boolean value 'true' will store in Country__c field which is a checkbox type of object Storing_Location__c, and when the 'China' radio button is checked then , the boolean value 'false' will store in that same field. 
Pramodh KumarPramodh Kumar
In your controller, after assigning Line 20 please make any DML operation. Without making DML the value will not be stored

If the size of str is zero then it will create a new record or it will update with the 0th index in the list
public class RadioButtonTestController 
{
    public Boolean Country{get; set;}
    public void checkSelectedValue()
    {
        
        system.debug('Selected value is: '+ Country);
    }
    
    public void showCountry()
    {
        String caseId = ApexPages.currentPage().getParameters().get('CaseID');
        list<Storing_Location__c> str = [Select Country__c from Storing_Location__c where CaseNumID__c=: caseId];
        Storing_Location__c obj = new Storing_Location__c();
        if(str.size()>0)
        {
            obj.Id = str[0].Id;
        }
        
        obj.Country__c = Country;
        upsert obj;
        system.debug('Selected value is:***********************:::'+Country);
        
    }
}



Thanks
Pramodh
 
Sukanya BanekarSukanya Banekar
Hi,

Your code is working fine it sets the value for INdia as true and china as false. Do one change in your constructor so that on load of page you will also get the value of boolean
 
public void checkSelectedValue()
    {
         Country= false;
        
        system.debug('Selected value is: '+ Country);
    }

Let me know if this helps.

Thanks,
Sukanya Banekar