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
Jagadeesh M 1Jagadeesh M 1 

Need help on validation rule/ apex code at least on check box need to selected

Hi All ,
I have a requirement On validation rule /Apex Code :
I need to select at lease one check box to be selected in the multiple row, if not we have to through as error message as must be select on check box.
Are let us know any possible apreouches.Your quick responce would be greatly apreacites...!!!

Thanks,
M. Jagadeesh
Shweta SoparkarShweta Soparkar
Let me get it correct. You want to throw an error message if the user does not select a value?
Jagadeesh M 1Jagadeesh M 1
Thanks for the reply ...
At least one check box need to be selected from the panel.  If any check box is not selected through an error.
Let me explain more:
User may add any no. of rows, each row having the one check box. If the user not select any check box click no go button, we have to diplay error that need to select at least one check box.
DixitDixit
if you need at least 1 of your checkbox fields to be selected, just do a validation rule that uses "OR" statement

OR(checkbox_1__c, checkbox_2__c, checkbox_3__c)
I don't remember if the checkbox should equals to "true" in validation, so if that doesn't work, try adding the " = True "
DixitDixit
Oops!, in apex code you need to check if the value was selected (if you have any variable linked to it on the controller)... make an "if" statement with

IF(checkbox1 || checkbox2 || checkbox3){
 /* do something */
} else {
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR, 'You need to pick at least 1 of the options');
                ApexPages.addMessage(errorMessage)
}

(need to have <apex:message />  <apex:messages /> in your VF page)
Jagadeesh M 1Jagadeesh M 1
As you said, I have changed my code but nothing is happening...

ICP_DFD_Member__c fcMember = (ICP_DFD_Member__c)ICP_DFD_Member__c.sObjectType.newSObject(null, true);
        fcMember.ICPDFD_Application__c=app.ID;
       list<ICP_DFD_Member__c> payeeList = [select Id,FIRST_NAME__c, LAST_NAME__c,GENDER__c, Payee__c from ICP_DFD_Member__c]; //where ICP_DFD_Member__c.ICPDFD_Application__c = app.ID
       system.debug('<<<EnterSaveNext and Payee Total List<<' + payeeList)   ;
            
        for(integer i =0; i < payeeList.size(); i++)
        {
            if(fcMember.Payee__c== false)
            {
                save(true);    
                     
            }
            else
             {
                 system.debug('<<<EnterSaveNext<<' )   ;
                     isSaveSuccessful = false;
                     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Please select the Payee'));
                
        
             }
         }



 
Jagadeesh M 1Jagadeesh M 1
I need to loop to find any check box is selected if not through the error message.
DixitDixit
If you need to loop through the list to find if a checkbox is true, your "if" statement should equal to "True" on any of the checkbox in an "OR" statement. 

On your loop, you are telling the code that if payee is NOT selected, it should save the register. If it is, it should throw an error that says "select the payee" ... Is the "payee" field a checkbox field? or which fields are the checkboxes?

(PS: is "cleaner" to put statements like " IF(!fcMember.Payee__c) more than IF(fcMember.Payee__c == False) )