+ Start a Discussion
Vinuthh SVinuthh S 

i have to select 5 checkbox..when i am trying to select the 6th checkbox it should display an error...i am sharing my code here...

<apex:page controller="AccountSelectClassController" sidebar="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
                <apex:pageBlockButtons >
                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table2"/>
            </apex:pageBlockButtons>
               <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">
 
                <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.MC_Title__c}" />
                    <apex:column value="{!accWrap.acc.MC_Content__c}" />
                    <apex:column value="{!accWrap.acc.MC_EffectiveDate__c}" />
                </apex:pageBlockTable>
               
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

Contoller

public class AccountSelectClassController{
 
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<MC_NewsFeed__c> selectedAccounts{get;set;}
    Date endDate = Date.newInstance(2015,02,21);
    Date startDate= Date.newInstance(2015,02,01);
    Integer maxRecord=5;
 
    public AccountSelectClassController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(MC_NewsFeed__c a: [select MC_Title__c,MC_Content__c,MC_EffectiveDate__c from MC_NewsFeed__c where (MC_FeedType_del__c = 'Recent GoLive') AND (MC_EffectiveDate__c >= :startDate) AND  (MC_ExpiryDate__c < :endDate)]  ) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
     public pageReference processSelected() {
    selectedAccounts = new List<MC_NewsFeed__c>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
        if(selectedAccounts.size()>5){
             Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'Invalid block selection. Cannot insert blocks below the selected block')); 
             return null;
        }
      return null;  
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public MC_NewsFeed__c acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(MC_NewsFeed__c a) {
            acc = a;
            selected = false;
        }
    }
}