0

Hi I have a consent statement and the user needs to check the box before registering. How can I refer the checkbox in the controller class and check the status of the checkbox to show an error message if checkbox is not checked.

VF Page

<apex:page controller="showUser" >
    <html>
        <head>
            <title>Program Registration</title>
        </head>
        <body>
            <apex:form >
                <apex:pageMessages />
                <apex:pageBlock title="Registration" >
                    <apex:inputCheckbox id="chkboxiagree"/>
                  I agree statement
                    <apex:pageBlockButtons >
                        <apex:commandButton action="{!saveRecord}" value="Register"/>
                    </apex:pageBlockButtons>
                </apex:pageBlock>
            </apex:form>
        </body>
    </html>
</apex:page>

Controller class

public class showUser {
    public String userName{set;get;}
    public List<Contact> storeDetails{set;get;}
    public Boolean chkboxiagree {set;get;}
    public static void saveRecord()  
    {
        showUser su = new showUser();
        try
        {
            if (su.chkboxiagree == 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 Member_COMMUNITY_Username__c like :su.userName];         
            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());
        }
    }
}

Can some one help me in this?

1 Answer 1

0

You have most of the right elements in place here, but a couple of small mistakes.

    public static void saveRecord()

This method should return a PageReference to control navigation after the action is executed.

            if (su.chkboxiagree == false) {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please agree to the consent statement'));
                    //return null;
            }       

This //return null; should be uncommented, because that's what you want to do - refresh the page to show your messages.

You'll also need to return a PageReference at the end of the successful process to navigate the user, and you certainly would want to present any exception as a PageMessage, not swallowing it into the debug log.

3
  • Thanks David, I made the changes as you mentioned, but no change. I understand that the code is not entering the if statement as I am getting event inserted. Can you please check whether the logic to refer the visual force component using the id is correct?
    – Kiran G
    Commented Dec 14, 2020 at 15:43
  • @KiranG Oh, I missed that. The variable is null because you didn't bind it to the checkbox component.
    – David Reed
    Commented Dec 14, 2020 at 15:57
  • Can you show me how to bind it?
    – Kiran G
    Commented Dec 14, 2020 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .