Reply
Regular Contributor
Suman M
Posts: 15
0
Accepted Solution

How to Pass selected Checkbox values from visualforce page to a TextArea Field in an Object.

 

Hi,

 

Please help me with the below scenario, I have Visualforce page through which I can select a value from Picklist and based on the Picklist value particular Checkboxes will be displayed on the page. My Requirment is when a checkbox value is selected and Save botton is clicked, then the checkbox value need to be stored in a TextArea Field in the respective Object.

 

Selected Checkbox values should be displayed in a TextArea Field ' PlacesVisisted__c ' in the Contact Object.

 

Please help me on this scenario. ThankYou.

 

Below is my Visualforce Page.

 

<apex:page standardController="Contact">
<apex:form >
<apex:pageBlock title="Update Contact Details">
 <apex:pageBlockButtons location="bottom">
  <apex:commandButton action="{!save}" value="Save"/>
  <apex:commandButton action="{!cancel}" value="Cancel"/>
 </apex:pageBlockButtons>
<apex:pageBlockSection id="NewPage" title="Select The Country">
    <apex:inputField value="{!Contact.Country__c}">
    <apex:actionSupport event="onchange" reRender="IndiaBlock, USBlock, ChinaBlock, AusesBlock"/>
    </apex:inputField>
    <apex:inputText value="{!Contact.Name}"/>
</apex:pageBlockSection> 
<apex:outputPanel id="IndiaBlock">
 <apex:pageBlockSection rendered="{!Contact.Country__c == 'India'}" id="SampleSendInfoBlock" title="Select Places Visited">
    <apex:inputCheckbox label="Agra"/>
    <apex:inputcheckbox label="Delhi"/>
 </apex:pageBlockSection>
</apex:outputPanel>
<apex:outputPanel id="USBlock">
 <apex:pageBlockSection title="Select Places Visited" rendered="{!Contact.Country__c == 'America'}" id="USPageBlock">
  <apex:inputCheckbox label="Newyork"/>
  <apex:inputCheckbox label="Washington"/>
 </apex:pageBlockSection> 
</apex:outputPanel>
<apex:outputPanel id="ChinaBlock">
 <apex:pageBlockSection title="Select Places Visited" rendered="{!Contact.Country__c == 'China'}" id="ChinaPageBlock">
 <apex:inputCheckbox label="Bejing"/>
 <apex:inputCheckbox label="Taiwan"/>
 </apex:pageBlockSection>
</apex:outputPanel>
<apex:outputPanel id="AusesBlock">
 <apex:pageBlockSection title="Select Places Visited" rendered="{!Contact.Country__c == 'Australia'}" id="AusesPageBlock">
 <apex:inputCheckbox label="Sydney"/>
 <apex:inputCheckbox label="Canberra"/>
 </apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Trusted Contributor
asish1989
Posts: 325
0

Re: How to Pass selected Checkbox values from visualforce page to a TextArea Field in an Object.

Hi

 The value of checkbox is always true or false...I think you want to save label of checkbox...am I correct.....?

 

Thanks

Asish Kumar Behera
Certified Salesforce Developer @
MindfireSolutions
|My Blog
Regular Contributor
Suman M
Posts: 15
0

Re: How to Pass selected Checkbox values from visualforce page to a TextArea Field in an Object.

Hi Asish,

 

thanks for the reply.

 

The value of the checkbox is False by default, when we select particular Checkbox then that repsective label has to be sent to Custom TextArea Field ( Places_Visted__c) in Contact Object. Is there any possible way for this.

Trusted Contributor
raseshtcs
Posts: 174
0

Re: How to Pass selected Checkbox values from visualforce page to a TextArea Field in an Object.

You would need to bind all the check boxes with some variable in your controller. Then check which all variables are true in the Class and those which are true just add them in a string field and update the contact record.

I would suggest to use a picklist multi select to do this instead, as it would be easier to maintain

<apex:inputCheckbox label="Newyork" value = "{!NY}"/>
  <apex:inputCheckbox label="Washington" value = "{!W}"/>

//In the Class
String total;

if(NY == True){
total = total+ 'NewYork';
}
if(W == True){
total = total + 'Washington';
}
c.PlacesVisisted__c = total;
Update c;
Regular Contributor
Suman M
Posts: 15
0

Re: How to Pass selected Checkbox values from visualforce page to a TextArea Field in an Object.

Hi Rasesh,

 

Thanks for the Reply.

 

If you dont mind could you please help me out by showing how I can bind a checkbox to variable. because in this case i'm creating a checkbox in the page. there is no instance of the checkbox in the object. 

 

if possible could you please take one checkbox and give me the code. I'll try to understand based on that. Thank You so much.

 

btw your name says u work for TCS. I'm also working for TCS. nice to meet you :)

Trusted Contributor
asish1989
Posts: 325
0

Re: How to Pass selected Checkbox values from visualforce page to a TextArea Field in an Object.

Hi

 Try this.....

  Here is my page 

<apex:page standardController="Contact" extensions="FStoringCheckBoxValueExtension">
    <apex:form >
        <apex:pageBlock title="Update Contact Details">
                <apex:pageBlockButtons location="bottom">
                    <apex:commandButton action="{!savingCheckBoxValue}" value="Save"/>
                    <apex:commandButton action="{!cancel}" value="Cancel"/>
                </apex:pageBlockButtons>
                
                <apex:pageBlockSection id="NewPage" title="Select The Country">
                    <apex:inputField value="{!Contact.Country__c}">
                        <apex:actionSupport event="onchange" reRender="IndiaBlock, USBlock, ChinaBlock, AusesBlock"/>
                    </apex:inputField>
                    <!--<apex:inputField value="{!Contact.LastName}"/>-->
                  <p> Last Name: <apex:inputText value="{!lastname}"/></p>
                </apex:pageBlockSection> 
                
                <apex:outputPanel id="IndiaBlock">
                    <apex:pageBlockSection rendered="{!Contact.Country__c == 'India'}" id="SampleSendInfoBlock" title="Select Places Visited">
                        <apex:outputLabel value="Agra" for="indiacheck">
                            <apex:inputCheckbox value="{!Indiaplaceist}" id="indiacheck1">
                                 <apex:actionSupport event="onclick" action="{!getSelected}" rerender="IndiaBlock"/>
                            </apex:inputCheckbox>
                        </apex:outputLabel> 
                        
                        <apex:outputLabel value="Delhi" for="indiacheck2">   
                            <apex:inputcheckbox id="indiacheck2" value="{!Indiaplace2nd}">
                                <apex:actionSupport event="onclick" action="{!getSelected}" rerender="IndiaBlock"/>
                            </apex:inputCheckbox>
                        </apex:outputLabel>
                        
                     </apex:pageBlockSection>
                </apex:outputPanel>
                
                <apex:outputPanel id="USBlock">
                    <apex:pageBlockSection title="Select Places Visited" rendered="{!Contact.Country__c == 'America'}" id="USPageBlock">
                        <apex:outputLabel value="Newyork" for="Newyorkcheck1">  
                            <apex:inputCheckbox value="{!Newyorkplaceist}" id="Newyorkcheck1">
                                <apex:actionSupport event="onclick" action="{!getSelected}" rerender="IndiaBlock"/>
                            </apex:inputCheckbox>
                         </apex:outputLabel>
                         <apex:outputLabel value="Washington" for="Newyorkcheck2">       
                             <apex:inputCheckbox value="{!Newyorkplace2nd}">
                                 <apex:actionSupport event="onclick" action="{!getSelected}" rerender="IndiaBlock"/>
                             </apex:inputCheckbox> 
                         </apex:outputLabel>       
                    </apex:pageBlockSection> 
                </apex:outputPanel>
                
                
                
        </apex:pageBlock>
    </apex:form>
</apex:page>

 My class is...

public class FStoringCheckBoxValueExtension {
    private  Contact contact;
    public String Indiaplaceist{get;set;}
    public String lastname{get;set;}
    public String Indiaplace2nd{get;set;}
    
    
    public String Newyorkplaceist{get;set;}
    public String Newyorkplace2nd{get;set;}
   
    public String resultString{get;set;}
    public FStoringCheckBoxValueExtension(ApexPages.StandardController controller) {
        Contact contact = new Contact();
    }
    public void savingCheckBoxValue()
    {   
        
        
        //upsert acct;
        //System.debug('@@@@@@@@@@@@@@@@@@@Indiaplace@@@@@@@@@@'+Indiaplace);
         System.debug('@@@@@@@@@@@@@@@@@@@resultString @@@@@@@@@@'+resultString );
         Contact contact = new Contact();
        contact.PlacesVisisted__c = resultString ;
        contact.LastName= lastname;
        insert contact;
    }    
    public PageReference getSelected(){
        System.debug('@@@@@@@@@@@@@@@@@@@Indiaplace@@@@@@@@@@'+Indiaplaceist);
        if(Indiaplaceist == 'true'){
            if(resultString !=null){
                resultString = resultString+' '+ 'Agra'; 
            }
            else{
                resultString = 'Agra'; 
             }      
           System.debug('@@@@@@@@@@@@@@@@@@@resultStringtestIf @@@@@@@@@@'+resultString ); 
         }
         else if(Indiaplace2nd == 'true'){
              if(resultString !=null){
                  resultString = resultString+' '+'Delhi' ;
              }
              else{
                  resultString = 'Delhi' ;  
              }     
          }
         else if(Newyorkplaceist == 'true') {
             if(resultString !=null){
                resultString = resultString+' '+'Newyork' ;  
             }
             else{
                 resultString = 'Newyork' ;  
              }   
         } 
         else if(Newyorkplace2nd == 'true') {
             if(resultString !=null){
                 resultString = resultString+' '+'Washington';
             }
              else{
                  resultString = 'Washington';
               }
         }
         else{
         }   
         
        return null;
    }
}

 Did this post answers your questions if so please mark it solved so that others get benifited..

 

   Thanks

Asish Kumar Behera
Certified Salesforce Developer @
MindfireSolutions
|My Blog
Regular Contributor
Suman M
Posts: 15
0

Re: How to Pass selected Checkbox values from visualforce page to a TextArea Field in an Object.

Thanks a lot Asish.. Got the solution and doing few changes for my requirment.. Thanks for your time.