3

I'm displaying a checkbox and a button on a Visualforce page. Upon clicking on the button, I want a checkbox field on a record (the current user's User record) to be updated with the value in the checkbox field on the Visualforce page and then take the user to another page. Here's the code I've currently got:

VF Page --

<apex:form>
        <apex:inputCheckbox label='By checking this box, you agree to our Terms and Conditions.'>
        </apex:inputCheckbox >
        <apex:commandButton value='Agree' action="{!updateUser}">
        </apex:commandButton>
    </apex:form>

Controller --

public PageReference updateUser() {
    Boolean acceptedCheckbox;
    User currentUser = [select Id, Accepted_Legal_Terms__c from User where Id = :UserInfo.getUserId() ];
    currentUser.Accepted_Legal_Terms__c = acceptedCheckbox;
    update currentUser;
    PageReference p;
    p = new PageReference('/apex/CC_Home');
    return p;
}

However, I am running into issues when trying to save the VF page, stating "Unknown property 'Controller.acceptedCheckbox'". Anyone have a guess as to what I'm missing here? Also, is the code I've laid out even the best way to handle this?

1 Answer 1

4

You need to actually define it as a property and bind it.

VisualForce

<apex:inputCheckbox value="{!hasAcceptedTerms}" label="whatever" />

Apex

public Boolean hasAcceptedTerms { get; set; }

public MyController()
{
    hasAcceptedTerms = false;
}
public PageReference updateUser()
{
    update new User(Id=UserInfo.getUserId(), Accepted_Legal_Terms__c=hasAcceptedTerms);
    return $Page.CC_Home;
}
1
  • Awesome, thanks much Adrian! (sorry about the delay)
    – John_Henry
    Jun 30, 2015 at 13:56

You must log in to answer this question.

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