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
Harjeet SinghHarjeet Singh 

how to restrict an user to proceed in absence of a validation not met

Hi All,
I have a requiremnt which i am describing below:
There is a button called "Continue" on UI. When an user clicks on it it will redirect to some other page. But prior to that one validations need to be done. As you people can see in the screen shot which i attached here is there is checkbox along with the name of each subscription. So my requirement is if an user does not select any single subscription with "Active" status and if user clicks on proceed button an error message must be thrown "Please select al least one active subscription".
I am attaching the corresponding code and Visual force page with the screen shot.
public with sharing class AccountActionsWrapper
{       
    public String contextItem{get;set;}   
    public set<Id> selectedContactIds{get;set;}
    public String accountName {get; set;}
    public string accountid;
    public String selectedActions{get;set;}
    public boolean AccountBool{get;set;}
    public List<Account> accounts {get; set;}
    public List<csord__Service__c> vLstServ{ get; set; }
    public Id acctId {get; set;}
    public integer size{get;set;}
    public Map<Id, List<csord__Service__c>> memIdServListMap { get; set; }
    public integer noOfRecords{get;set;}
    public Date dat {get;set;}
    Set<Id> SubIds = new Set<Id>();
    public boolean Display{get;set;}
     Map<Id, csord__Subscription__c> subMap = new Map<Id, csord__Subscription__c>();
    public List<csord__Subscription__c>vLstSubscriptions{get;set;}
    public List<wrapSubscription> wrapSubscriptionList {get; set;}
    public List<csord__Subscription__c> selectedSubscriptions{get;set;} 
    public Boolean isCancel{get;set;}
    public String query {get; set;}
    public String dateError {get;set;}
    public String currentDateValue { get; set; }

    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
           size =20; 
           accountid=ApexPages.currentPage().getParameters().get('id');
           selectedContactIds= new Set<Id>();       
           setCon= new ApexPages.StandardSetController( [select Id, Name,csord__Status__c,MACD_in_progress__c , csord__Account__c from csord__Subscription__c where csord__Account__c=:accountid] );
           this.setCon.setpageNumber(1);
           setCon.setPageSize(size);
           noOfRecords = setCon.getResultSize();            
            }
            return setCon;
        }set;
    }
    
     public AccountActionsWrapper() 
     {
       selectedActions='Take Over';
       if(selectedActions=='Take Over')
       {
          AccountBool=true; 
       } 
      }
        
    public List<SelectOption> getActions() {
        List<SelectOption> Actions= new List<SelectOption>();
        Actions.add(new SelectOption('Take Over','Take Over'));
        Actions.add(new SelectOption('Invoice Switch','Invoice Switch'));
        Actions.add(new SelectOption('Move','Move'));
        return Actions;
    }
    public void getDisplayAccount()
    {
        if(selectedActions!='Take Over')
        {
           AccountBool=false; 
        }
        else
        {
             AccountBool=true;
        }
    }
    
    //function to get date value    
     public void getValue(){
      System.debug('*************'+currentDateValue);
      
    
     }
     
     //Function to retrieving account detail in pop-up upon clicking on Lookup in VF page
     public PageReference runQuery()
    {
        List<List<Account>> searchResults=[FIND :query IN ALL FIELDS RETURNING Account (id, name, billingstreet, billingcity, billingpostalcode)];
        accounts=searchResults[0];
        return null;
    }   
    
    public void doSelectItem()
    {
      this.selectedContactIds.add(this.contextItem);
    } 
    public void doDeselectItem()
    {
      this.selectedContactIds.remove(this.contextItem);
    } 
    public Integer getSelectedCount()
    {
      return this.selectedContactIds.size();
    }
    public void doNext()
    { 
      if(this.setCon.getHasNext())
         this.setCon.next();
    } 
    public void doPrevious()
    {
      if(this.setCon.getHasPrevious())
         this.setCon.previous();
    }
    public void doStart() 
    { 
      if(this.setCon.getHasPrevious())
         this.setCon.previous();
    }
    public void doEnd() 
    { 
      if(this.setCon.getHasNext())
         this.setCon.next();
   }
     
    public List<wrapSubscription> getSubscriptions()
    { 
        
        List<wrapSubscription> rows = new List<wrapSubscription>(); 
        //vLstSubscriptions = [select Id, Name,csord__Status__c, csord__Account__c,(select Id,Billing_Account_Number__c from csord__Services__r) from csord__Subscription__c where csord__Account__c=:accountid limit 100];
        for(sObject r : this.setCon.getRecords())
        {
            csord__Subscription__c c = (csord__Subscription__c)r;
            wrapSubscription row = new wrapSubscription(c,vLstServ);
            if(this.selectedContactIds.contains(c.Id))
            {
                row.selected=true;
            }
            else
            {
                row.selected=false;
            }
            rows.add(row);
        }
        return rows;
         
    }
    public PageReference cancel()
    {
        return new PageReference('/'+accountid);
    }
    public Boolean getHasPrevious()
    { 
        return this.setCon.getHasPrevious();
    } 
    public Boolean getHasNext()
    {
       return this.setCon.getHasNext(); 
    }  
    public Integer getPageNumber()
    {
       return this.setCon.getPageNumber();
    } 
    Public Integer getTotalPages()
    {
        Decimal totalSize = this.setCon.getResultSize();
        Decimal pageSize = this.setCon.getPageSize();
        Decimal pages = totalSize/pageSize; 
        return (Integer)pages.round(System.RoundingMode.CEILING);
    }  
    public class wrapSubscription
    { 
        public csord__Subscription__c sub {get; set;}        
        public Boolean selected {get; set;}
        public List<csord__Service__c> Serv{get;set;}
        public wrapSubscription(csord__Subscription__c c,List<csord__Service__c> vLstServ)
        {
            this.sub=c;
            this.Serv=vLstServ;
            this.selected=false;
        }
 
    }
}
<apex:page docType="html-5.0" controller="AccountActionsWrapper" sidebar="false" showHeader="false" id="pg" >

<head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
</head>


<!--for mm/dd/yyyy format-->
<!--<script>
       $(function(){
        $('.datepicker').datepicker({
             minDate: 0 
         }).on('changeDate', function(ev){
            $('#sDate1').text($('#datepicker').data('date'));
            $('#datepicker').datepicker('hide');
        });
    
    })
</script>-->

<!--for dd/mm/yyyy format-->
<script>
       $(function(){
        $('.datepicker').datepicker({ minDate: 0 , dateFormat: 'dd/mm/yy' }).on('changeDate', function(ev){
            $('#sDate1').text($('#datepicker').data('date'));
            $('#datepicker').datepicker('hide');
        });
    
    })
</script>


<script>
    var newWin=null;
    function openLookupPopup(name, id)
    {
        var url="/apex/LookupExamplePopup?namefield=" + name + "&idfield=" + id;
        newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
        if (window.focus) 
        {
            newWin.focus();
        }
            
        return false;
    }
                  
    function closeLookupPopup()
    {
       if (null!=newWin)
       {
          newWin.close();
       }  
    }
    
  
    
     function doCheckboxChange(cb,itemId){ 
            if(cb.checked==true){
                aSelectItem(itemId);
            }
            else{
                aDeselectItem(itemId);
            }
        }
</script>

    <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:pageBlockSection >    
        <apex:pageBlockSectionItem >
        Select the Action:
        <!--<apex:outputPanel>-->
          <apex:selectList value="{!selectedActions}" multiselect="false" size="1" >
                <apex:selectOptions value="{!Actions}"/>
                <apex:actionSupport event="onchange" action="{!getDisplayAccount}" reRender="panel"/>             
            </apex:selectList>
           </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
      <apex:outputPanel >        
       <apex:pageBlockSection >     
           <apex:pageBlockSectionItem >          
           <apex:outputLabel value="Target Account"/>        
           <apex:outputPanel id="panel" >
            <apex:inputHidden value="{!acctId}" id="targetId"/>
            <apex:inputText size="40" value="{!accountName}" id="targetName" onFocus="this.blur()" disabled="{!!AccountBool}"/>&nbsp;
            
            <apex:outputLink rendered="{!AccountBool}" onclick="openLookupPopup('{!$Component.targetName}', '{!$Component.targetId}'); return false">Lookup</apex:outputLink>                    
          </apex:outputPanel>          
      </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
      </apex:outputPanel>
               <apex:PageBlockSection >
            <apex:pageBlockSectionItem >
                <!--Date: <apex:input type="date" value="{!dat}" required="false"/>--> 
                Effective From:<apex:inputText id="datepicker" styleClass="datepicker"  value="{!currentDateValue}" required="false"/>              
            </apex:pageBlockSectionItem>           
            <!--<apex:input value="{!myDate}" id="theTextInput" type="date"/>-->
        </apex:pageBlockSection>       
    </apex:pageBlock>   
        
        <!-- handle selected item -->
        <apex:actionFunction name="aSelectItem" action="{!doSelectItem}" rerender="mpb">
            <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
        </apex:actionFunction>
 
        <!-- handle deselected item -->
        <apex:actionFunction name="aDeselectItem" action="{!doDeselectItem}" rerender="mpb">
            <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
        </apex:actionFunction>        
         <apex:pageBlock title="Select Subscriptions" id="mpb">        
            <apex:pageblockSection title="Subscriptions" collapsible="false" columns="1" >
            <apex:pageBlockTable value="{!Subscriptions}" var="c">
                <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>                       
                            <apex:inputCheckbox value="{!c.selected}" id="inputId" disabled="{!NOT(c.sub.csord__Status__c=='Active') && (c.sub.MACD_in_progress__c=False)}" onchange="doCheckboxChange(this,'{!c.sub.Id}')"/>
                            
                    </apex:column>           
                 <apex:column headerValue="Subscription Name" >                       
                       <apex:outputLink value="/{!c.sub.Id}" target="_blank"> {!c.sub.Name} </apex:outputLink>
                   </apex:column>
                <apex:column value="{!c.sub.csord__Status__c}"/>                
                           
            </apex:pageBlockTable>
            </apex:pageblockSection>
           
        <apex:commandButton value="First" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
        <apex:commandButton value="Previous" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
        <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
        <apex:commandButton value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
        <apex:commandButton value="Last" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
         <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Continue "  />
                <apex:commandButton value="Back" action="{!cancel}" immediate="true" />
            </apex:pageBlockButtons>
        </apex:pageBlock>        
    </apex:form>
</apex:page>
screen shot

This forum has helped me out earlier also and i am very hopeful that you people will help me out this time also.
Please fine tune my code wherever applicable.

Many thanks in advance.