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
bharath kumar 52bharath kumar 52 

Using checkbox in vf page to select one record coming from the wrapper class

Hi Guys,
i am very close to developing a vf page on which i get a set of records from which i need to select "one and only one".
The challenges i faced are that i am able to select multiple records but not one. So tried to use radio button but that wouldn;t come for the save.
So pelase tell me how i can select single record and save it.
Below is my code and screenshot : 
<apex:page controller="wrapcontactlist">
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $j = jQuery.noConflict();
        //$j = jQuery.noConflict();

   $j(document).ready(function() {

    alert('Inside function');
var last_valid_selection = null;
alert('Inside function1'+last_valid_selection);
         $j('#checkb').change(function(event) {
alert('Inside event function1'); // going till here but not below
            if ($j('#checkb').val().length > 1) {
						alert('Cannot choose! already oversized ID: fgbrt46456456546456cdfedf'+$j(this).val().length)
              $j(this).val(last_valid_selection);
            } else {
              last_valid_selection = $j(this).val();
            }
          });
        });
   

        /*$j(document).ready(function() {

          var last_valid_selection = null;

         $j('#checkb').change(function(event) {

            if ($j(this).val().length > 3) {
						alert('Cannot choose! already oversized ID: fgbrt46456456546456cdfedf'+$j(this).val().length)
              $j(this).val(last_valid_selection);
            } else {
              last_valid_selection = $(this).val();
            }
          });
        });*/
    </script>
    </head>
    
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    
                        
                   
                            <apex:inputCheckbox value="{!c.selected}" id="checkb"/>

          
                           
                        
                    
                    
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!c.con.Name}" />
                <apex:column value="{!c.con.Email}" />
                <apex:column value="{!c.con.Phone}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

User-added image
 
Best Answer chosen by bharath kumar 52
SonamSonam (Salesforce Developers) 
There are again 2 ways to implement it .

First / Suggested way is the controller class 

- In your apex class, you need to check if the user selected more than multiple 
records and throw an apex page error when he clicks on Process Selected button. 


- You can use the same way using action support if you want the user to get the error while selecting the other record immediately.

<apex:page controller="controller">
<script type="text/javascript">
   var contactIdLst = [];
   function restrictPickList(contactId) {
        
       contactIdLst.push(contactId);
       if(contactIdLst.length > 1) {
           alert('You cannot select multiple contacts');
       } 
       
   
   }
</script>   
   
<apex:form id="frm">
<apex:pageMessages ></apex:pageMessages>
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Process Selected" action="{!processSelected}" reRender="frm"/>
        </apex:pageBlockButtons>    
        
        <apex:pageBlockTable value="{!contacts}" var="c" id="table">
            <apex:column >
                <apex:inputCheckbox value="{!c.selected}" />
            </apex:column>
            <apex:column value="{!c.con.name}"/>
            <apex:column HeaderValue="Confirm" value="{!c.con.abhiman__Confirmed__c }"/>
            
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>




public class controller {

  
    public PageReference processSelected() 
    {
        try
        {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<Opportunity > selectedContacts = new List<Opportunity >();

        //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
        for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }

        if(selectedContacts.size() > 1) {
        
            ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR,'You cannot select multiple contacts'));
        }
        
        


        }
        catch(Exception ex)
        {}
        
        return null;
                
    }


  
}

- You can use custom JS to check if the user selected multiple records and throw a JS error / alert butin that case you need to handle 
the error display and make sure when the user unselect the record it should be counted and should reduced the array length.