+ Start a Discussion
RovRov 

Onchange of Checkbox value, it is not getting changed in database

Here hidden_field__c is a checkbox.

When in the VFP if user changes the checkbox to true in the database it still shows as false and vice-versa

Can someone please point out what's missing in my code.?

This is my simple code

<pre>

public class dataTableCon {

    List<Account> accounts;

    public List<Account> getAccounts() {

        if(accounts == null) accounts = [select name, owner.name,hidden_field__c from account limit 10];

        return accounts;

    }

}

</pre>

<pre>
<apex:page controller="dataTableCon" id="page">
   
    <apex:form >
   
    <apex:pageBlock id="theBlock">
   
    <apex:dataTable value="{!accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
   
        <apex:column >
            <apex:facet name="header">Private</apex:facet>
            <apex:inputCheckbox value="{!account.hidden_field__c}" >
             <apex:actionSupport event="onchange" rerender="theBlock"/>
           </apex:inputCheckbox>           
        </apex:column>
   
       
        <apex:column >
            <apex:facet name="header">Name</apex:facet>
            <apex:outputText value="{!account.name}"  >
        </apex:column>
   
        <apex:column >
            <apex:facet name="header">Owner</apex:facet>
            <apex:outputText value="{!account.owner.name}"  >
        </apex:column>
       
    </apex:dataTable> 
   
    </apex:pageBlock>
    </apex:form>
</apex:page>  

</pre>
Best Answer chosen by Rov
GlynAGlynA
<pre>
<apex:column rendered="{!!account.hidden_field__c}">
            <apex:facet name="header">Private</apex:facet>
            <apex:inputCheckbox value="{!account.hidden_field__c}" >
                <apex:actionSupport event="onchange" action="{!saveAccount}" rerender="theBlock">
                    <apex:param name="accountId" value="{!account.Id}"/>
                </apex:actionSupport>
            </apex:inputCheckbox>
        </apex:column>

    public Id accountId { get; set; }
    public void saveAccount()
    {
        Map<Id,Account> map_Accounts = new Map<Id,Account>( accounts );
        update map_Accounts.get( accountId );
    }
</pre>

All Answers

izay.ramos-irizarry1.3893936724146558E12izay.ramos-irizarry1.3893936724146558E12
Rov,

You need to add a save method in you controller to save your changes and a button in your vf page to call that save method.

in controller:
public pageReference save(){
    try{
        insert accounts;
    }catch(Exception e){
        System.debug(e.getMessage());
    }
    return null;
}


in vfPage within Page Block:
<apex:pageBlockBottons>
    <apex:commandButton action="{!save}" value="Save" id="theButton"/>
</apex:PageBlockButtons>
GlynAGlynA
The previous post is correct, however you should update the Accounts, not insert them.

-Glyn
RovRov
Thanks for the response. Sorry to respond late.

Actually I don't want this happening on the click of save button.

When the checkbox is clicked, I want to pass the page parameters to the controller,  Capture the row Id and check if the checkbox is set to true or false.

If it is true then not show it in the datatable. I am not deleting it from the data base, just not showing it on the data table to a certain group of people.

Any suggestions.

Thanks.!!
GlynAGlynA
rov,

The VF code in the next post hides the columns of the dataTable if the record has the Hidden_Field__c checkbox checked.  Let me know if this does what you want.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy.blogspot.com
Twitter: @GlynAtClosedWon
GlynAGlynA
<pre>
<apex:page controller="dataTableCon" id="page">
    <apex:form >
    <apex:pageBlock id="theBlock">

    <apex:dataTable value="{!accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">

        <apex:column rendered="{!!account.hidden_field__c}">
            <apex:facet name="header">Private</apex:facet>
            <apex:inputCheckbox value="{!account.hidden_field__c}" >
             <apex:actionSupport event="onchange" rerender="theBlock"/>
           </apex:inputCheckbox>
        </apex:column>

        <apex:column rendered="{!!account.hidden_field__c}">
            <apex:facet name="header">Name</apex:facet>
            <apex:outputText value="{!account.name}"  >
        </apex:column>

        <apex:column rendered="{!!account.hidden_field__c}">
            <apex:facet name="header">Owner</apex:facet>
            <apex:outputText value="{!account.owner.name}"  >
        </apex:column>

    </apex:dataTable>

    </apex:pageBlock>
    </apex:form>
</apex:page>
</pre>
RovRov
Thanks for your response.

No i tried rendering before, this hides the row from the DataTable, but in the database, the checkbox doesn't get updated.

eg: if i make the hidden_field__c 'true' for row1 in the datatable, in the database the hidden_field__c must get updated to true and vice-versa. 

Basically i am trying to do is dynamically making a record private or public. 

I have created a VFP that displays a datatable of related custom_comments__c. since each parent record would have many comments, it's not a good user experience to go into individual chidl record to make it as private/public.The hidden_field__c checkbox is accssible only to certain users who can make the child  record as public/private. If it is marked as private, these ceratin users can view the record but other user's cannot view it.

I have these certain users in a permission set.

GlynAGlynA
OK, I understand now.  You need a way to save the account record.  The next post has some code - a change to the actionSupport so that it calls a method in the controller, "saveAccount", with a paramter that identifies which account to save - and the controller code needed to make that work.

Let me know if this helps.

-Glyn

GlynAGlynA
<pre>
<apex:column rendered="{!!account.hidden_field__c}">
            <apex:facet name="header">Private</apex:facet>
            <apex:inputCheckbox value="{!account.hidden_field__c}" >
                <apex:actionSupport event="onchange" action="{!saveAccount}" rerender="theBlock">
                    <apex:param name="accountId" value="{!account.Id}"/>
                </apex:actionSupport>
            </apex:inputCheckbox>
        </apex:column>

    public Id accountId { get; set; }
    public void saveAccount()
    {
        Map<Id,Account> map_Accounts = new Map<Id,Account>( accounts );
        update map_Accounts.get( accountId );
    }
</pre>
This was selected as the best answer
RovRov
Thanks for walking me through this. Much appreciated!!!

i added the assignTo in the param tag. Without the  assignTo  i was getting the null pointer exception.

<apex:param name="accountId" value="{!account.Id}" assignTo="{!accountId}"/>
GlynAGlynA
Whoops!  Glad you caught the "assignTo" problem.  Glad it all worked for you!

-Glyn