+ Start a Discussion
Douglas TrasterDouglas Traster 

Visualforce Checkbox updating a custom field

I have created a visual force page that includes a checkbox.  How do I go about updating a custom field on another object with the result of the vf checkbox?  True (checked) = Answer1, False(not checked) = Answer 2?

I have read about wrapping etc, but not sure if this is the correct process or not?  

Please Help!!!!!!
Best Answer chosen by Douglas Traster
Krishna SambarajuKrishna Sambaraju
Where is the Id of the record that you want to update? Are you storing the value any where on the VF page or a controller variable. If you know then you can create a method in the controller to update that object based on the checkbox.
Controller:
public class someController{
	public boolean IsChecked {get; set;}
	public string recordId {get; set;}
	public someController()
	{
		IsChecked = false;
	}
	public pageReference updateObject()
	{
		recordId = 'some value'; //set the id of the record to update
		objectName obj = [select Id from objectName where Id = :recordId];
		if (IsChecked)
		{
			obj.field1__c = 'Answer1';
		}	
		else
		{
			obj.field1__c = 'Answer2';
		}

		update obj;
		return null;
	}
}

VF Page:
<apex:page controller="someController">
	<apex:form>
		<apex:inputCheckbox value="{!IsChecked}" onchange="update_js;"/>
		<apex:actionFunction name="update_js" action="{!udpateObject}"/>
	</apex:form>
</apex:page>
Change the objectname and fieldnames accordingly. Hope this helps