0

I am trying to show an error message when the checkbox is not checked. How do I pass the checkbox parameter from visualforce page to controller so that I can compare the status of checkbox and display an error message accordingly.

VF Page

<apex:page controller="showUser">
    <html>
        <head>
            <title>Program Registration</title>
        </head>
        <body>
            <apex:form >
                <apex:pageBlock title="Registration" >
                        <apex:pageMessages />
                <!--    <apex:inputField value="{!event.Authorized_by__c}"/> -->
                    <br/>
                    <br/>
                        <apex:inputCheckbox id="mycheckbox" value="{!chkBoxAgree}" required="True"/>Consent Statement here 
                    <apex:pageBlockButtons >
                        <apex:commandButton action="{!saveRecord}" value="Register"/>
                    </apex:pageBlockButtons>
                </apex:pageBlock>
            </apex:form>
        </body>
    </html>
</apex:page>

Controller

public without sharing class showUser {
    public String userName{set;get;}
    public List<Contact> storeDetails{set;get;}
    public Boolean chkBoxAgree{set;get;}
    public static PageReference saveRecord()  
    { 
        showUser su = new showUser();
        try
        {
                    if (su.chkBoxAgree == false) 
                    {
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please agree to the consent statement'));
                        return null;
                    }       
                    Event_Registrations__c event = new Event_Registrations__c();
                    su.userName= UserInfo.getUserEmail();
                    system.debug(su.userName);
                    su.storeDetails = [select Store__c, AccountId, Account.Name from contact where Email like :su.userName];       
                    system.debug(su.storeDetails);
                    event.Store_No__c = su.storeDetails[0].Store__c;
                    event.Account__c = su.storeDetails[0].AccountId;
                    event.Program_Event__c = 'a1G3D000001Re07UAC';    
                    insert event;
                    Account accountData = [select Id from Account where ID = :event.Account__c limit 1];
                    accountData.Registered_for_MedSask__c = true;
                    update accountData;  
        }
        catch(Exception e)
        {
            System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        return null;
    }
}
0

You don't need the su variable. Get rid of showUser su = new showUser(); and all the su. accesses. The variables bound in your page are directly accessible in your controller. The saveRecord method must not be static.

3
  • I removed showUser su = new showUser(); and the su. But now I am getting variable does not exist error for the all the class variables
    – Kiran G
    Dec 15 '20 at 2:52
  • @KiranG I just noticed the method is static. It must not be static in order to access the class variables.
    – sfdcfox
    Dec 15 '20 at 3:12
  • @KiranG For the unit test, the test method is static, but you will create your controller in the unit test. See this answer for an example of how to do that.
    – sfdcfox
    Dec 15 '20 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.