+ Start a Discussion

Suggestions on how to make checkbox fields "sticky"?

I have a custom object with a checkbox field that is defaulted to "checked". When creating a new record, I would like to retain the state of the checkbox (either checked or unchecked) from the previous new record rather than always reverting to the default state of "checked".


I'm using a VF page and a custom controller extension based on the example provided here: http://www.fishofprey.com/2012/03/salesforce-save-and-new-method-for.html to create a "Save & New" type of rapid data entry form. 


Now, I just need the checkbox field on the VF page to be "sticky" so that it only has to be selected once for a batch of work that is all "unchecked".


Any suggestions on how to do that?


cookie on javascript?


So I've gone down the path of Javascript and cookies, but I'm coming up short and I cannot figure out what's wrong.


In the following code, I've been able to successfully create a cookie called "apex__isValid" and set it to true or false based on the state of a checkbox field called "IsValid__c". I use the onClick event to invoke a Javascript function called "setCookie" and it all works like a charm.


The problem is with the "checkCookie" function. My goal is to set the "checked" value of the IsValid__c field based on the current value of the apex_isValid cookie. I have a Javascript function that runs on the onLoad event and invokes the checkCookie function. The line in the Javascript that does not seem to be working is:


      document.getElementById("{!$Component.ValidCheckbox}").checked = isValid;


The IsValid__c field is not affected by the Javascript and I suspect the above line is not working or is not being used in its proper context, but I have no idea what to try next. Please help.



Enclosed is my complete page. I can include the controller extension too if anyone would like to see it. The controller extension is being used to provide a "Save & New" capability for my custom object.


<apex:page standardController="Proxy__c" extensions="CustomExtensionController" sidebar="false">

    <script type="text/javascript" src="/js/functions.js"></script>
    <script src="/soap/ajax/11.1/connection.js"></script>
    <script language="javascript">

    function getCookie(c_name)
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
      if (x==c_name)
        return unescape(y);
    function setCookie(c_name,value,exdays)
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
    function checkCookie()
    var isValid=getCookie("apex__isValid");
    if (isValid!=null)
//    alert(isValid);
      document.getElementById("{!$Component.ValidCheckbox}").checked = isValid;
//Function below checks the isValid cookie onLoading of the page
    var previousOnload = window.onload;        
    window.onload = function() {
        if (previousOnload) {

  <apex:sectionHeader title="Proxy Edit"
                      subtitle="New Proxy"/>
  <apex:form >
    <apex:pageBlock title="Proxy Edit" id="thePageBlock"
      <apex:pageMessages />
      <apex:pageBlockButtons >
        <apex:commandButton value="Save"  action="{!saveAndNew}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
        <apex:pageBlockSection title="Proxy Information" columns="1" id="thePageBlockSection">
          <apex:inputField value="{!Proxy__c.Policy_Number__c}"/>
          <apex:inputField value="{!Proxy__c.Receipt_Date__c}"/>
          <apex:inputCheckbox value="{!Proxy__c.IsValid__c}" id="ValidCheckbox" onclick="setCookie('apex__isValid',document.getElementById('{!$Component.ValidCheckbox}').checked,3)"/>       
        <apex:pageBlockSection title="System Information" columns="1">
          <apex:inputField value="{!Proxy__c.Scanned_By__c}"/>
          <apex:inputField value="{!Proxy__c.Scan_Date__c}"/>