function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NewInternNewIntern 

Simple VF question: Saving checkbox selection after VF page reload / refresh

I have a visualforce page with standard controller 'contact' and an extension class. This visualforce page appears in a section on each Contacts record page. The page renders several SObject checkboxes. 

 

The issue is when a user clicks the checkbox, the selection is not remembered when the Contacts record page and therefore the VF page section are reloaded. How do I code the VF page so that when the Contact record is opened again, the checkboxes that were checked remain checked?

 

Please help.

Best Answer chosen by Admin (Salesforce Developers) 
NewInternNewIntern

Well, I just used the following code in my Apex class:

 

public class CustomApexClass
{
public Contact cont { get; set; }
public ApexPages.StandardController c;

public VFforEmailTemplateController(ApexPages.StandardController controller)
{
this.cont = (Contact)controller.getRecord();
c = controller;

}
public PageReference saveRecord(){
try{
c.save();
}
catch(Exception e){
}
return null;
}
}

 This works like a charm.

All Answers

sieb4mesieb4me
sorry i dont get the issue, can you explain this part plz


The issue is when a user clicks the checkbox, the selection is not remembered when the Contacts record page and therefore the VF page section are reloaded.
NewInternNewIntern

Sorry for the lack of clarity. 

 

I meant the selection is not remembered when the Contacts record page is reloaded. The checkbox is on a VF page and this VF page I have put in a section on the Contacts records. When I revisit the Contacts record, the VF page gets refreshed and the earlier user input of checking the checkbox is lost.

 

Essentially, I want  to 1) navigate to Contact tab and view the record, 2) select the checkbox in the VF page that you see, 3) go elsewhere and come back to Contacts to see the same checkbox selected. Currently, the checkbox selection gets refreshed or removed when I refresh the Contacts record.

 

Here's my code:

 

<apex:page standardController="Contact" extensions="VFforEmailTemplateController">

<apex:form >
<apex:pageBlock id="theBlock">
</apex:column>
<apex:column headerValue="On Approved List" width="111">
<apex:inputField value="{!c.EJF_On_Approved__c}">
<apex:actionSupport event="onclick" reRender="theBlock" action="{!check_selection}"/>
</apex:inputfield>
</apex:column>

 

I did not write any code in the controller extension. Here EJF_On_Approved is the name of the SObject which is a custom checkbox.

NewInternNewIntern

Thanks for those links. However, there are way too complicated for me. 

 

My requirement is simple. I don't even want to perform any action when a checkbox is selected.

 

All I want for is when the user checks this SObject custom checkbox 'EJF_On_Account' displayed in my VF page (which is on the page layout of Contacts), this selection is remembered. So the next time I view the contact record or navigate back to the contact record, I see the checkbox checked just like the user last saw, not unchecked as I see it now each time the VF page is refreshed. 

 

I know the solution is not complicated. I'm actually surprised that there is no inherent/auto saving of a change made to a custom object field.

 

Need help.

Avidev9Avidev9
You may want to update your variable i.e. "c" from "check_selection" method of controller by doing something like "update c;" from the above specified method.
sieb4mesieb4me
my guess is you have to use pagereference class to maintain state, see this issue here, same problem as you with solution.

http://boards.developerforce.com/t5/Visualforce-Development/Visualforce-page-loses-field-value-when-refreshing/td-p/166465
sieb4mesieb4me
is your prob resolved? if so , what did you do?
NewInternNewIntern

Well, I just used the following code in my Apex class:

 

public class CustomApexClass
{
public Contact cont { get; set; }
public ApexPages.StandardController c;

public VFforEmailTemplateController(ApexPages.StandardController controller)
{
this.cont = (Contact)controller.getRecord();
c = controller;

}
public PageReference saveRecord(){
try{
c.save();
}
catch(Exception e){
}
return null;
}
}

 This works like a charm.

This was selected as the best answer