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
yarramyarram 

How to Insert Multi Check Boxes selected values into one Text Field.

Hi All,

 

I have one VF Page, That contains 30 Checkboxes(Check Boxes List), I select first 3  Check Boxes and Click on save Button on VF page.

 

Values of 3 check boxes are 1,2 and 3 are saved into one Texed Field.

 

Like...Text Field is  Blocks : 1,2,3

 

 

public Blocks__c Block= new Blocks__c(); 

Public Void blockSave()

{

       Insert Block;

}

 

public String Boxes{get;set;}
public List<SelectOption> getCheckBoxes()
{
List<SelectOption> CheckboxLists = new List<SelectOption>();
for(Integer i=1;i<=30;i++)
{
CheckboxLists.add(new SelectOption(String.valueOf(i),String.valueOf(i)));
}
return CheckboxLists;
}

 

 

// VF Page Code----------------
<apex:selectCheckboxes value="{!Boxes}">
<apex:selectOptions value="{!BlocknoLists}"/>
</apex:selectCheckboxes>

 

Please give help, How can I Achive.

 

Thanks,

Yarram.

Best Answer chosen by Admin (Salesforce Developers) 
yarramyarram

Hi Premanath,

 

i got the solution, Now its inserts the multi selected Check Boxes values into one TextField.

 

I was achived like this.....

 

Block.Blocks__c=String.valueOf(Boxes);

insert Block;

 

Thanks for your suggistions..

 

Thanks,

yarram.

All Answers

PremanathPremanath

You can try like this

May be you can achive, This can help you...

 

<apex:page controller="sampleCon">
<apex:form >
<apex:selectCheckboxes value="{!countries}">
<apex:selectOptions value="{!items}"/>
</apex:selectCheckboxes><br/>
<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
</apex:form>
<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p>
<apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>

 

public class sampleCon {
String[] countries = new String[]{};

public PageReference test() {
return null;
}

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('US','US'));
options.add(new SelectOption('CANADA','Canada'));
options.add(new SelectOption('MEXICO','Mexico'));

return options;
}

public String[] getCountries() {
return countries;
}

public void setCountries(String[] countries) {
this.countries = countries;
}
}

yarramyarram

Hi Premanath,


Thanks for replying. FYI

When i am trying to insert multiselect check boxes values into TextField through Below mentioned code.

It gives Error Like "Illegal assignment from LIST<String> to String"



public Blocks__c Block= new Blocks__c();

public String[] Boxes= new String[]{};

  public String[] getBoxes()
  {
    return Boxes;
  }
    public void setBoxes(String[] r)
    {
    this.Boxes= Boxes;
    }
public List<SelectOption> getCheckBoxes()
{
List<SelectOption> CheckboxLists = new List<SelectOption>();
for(Integer i=1;i<=30;i++)
{
CheckboxLists.add(new SelectOption(String.valueOf(i),String.valueOf(i)));
}
return CheckboxLists;
}

 Public Void blockSave()
{
    Block.Blocks__c=Boxes;//Blocks__c is Text Field.
       Insert Block;
}

// VF Page Code----------------
<apex:selectCheckboxes value="{!Boxes}">
<apex:selectOptions value="{!BlocknoLists}"/>
</apex:selectCheckboxes>

 


Please give me guidance. where i Did mistake.

 

Thanks,

Yarram.

 

PremanathPremanath

Hi

 

The Error is :

public Blocks__c Block= new Blocks__c();

Here you are takjing multiple things.

 

 Block.Blocks__c=Boxes;

 

Take one for loop for Boxes insert one by one

 

like this but not exactly

 

for(integer i=0;i<Boxes.size();i++)

Block.Blocks__c=Boxes[i];

 

 

yarramyarram

Hi Premanath,

 

i got the solution, Now its inserts the multi selected Check Boxes values into one TextField.

 

I was achived like this.....

 

Block.Blocks__c=String.valueOf(Boxes);

insert Block;

 

Thanks for your suggistions..

 

Thanks,

yarram.

This was selected as the best answer
PremanathPremanath

Thankss for Sharing it.

 

babloo123babloo123
Hi Yarram,

                I have a similar requirement can you please help me with the code as I am unable to get the controller code clearly. Your help would be much appreciated.