0

Hi I have following code for visual force and apex controller which renders the information about the 4 quarters in current year.. customer visit the page and select the check boxes and submit their preference.

It renders the page but what customer want is whenever they visit the page again the previously selected choices should be shown to them.

Visual Force to display Checkboxes

<apex:selectCheckboxes value="{!RegPeriod}" required="true">
                    <apex:selectOptions value="{!RegistrationPeriod}"/> 
</apex:selectCheckboxes>

Apex Controller:

public List<String> regPeriod = new List<String>();
    public List<String> getRegPeriod(){
        return regPeriod;
    } 
    public void setRegPeriod(List<String> RegPrd){
        regPeriod.addAll(RegPrd);
    }

public List<SelectOption> RegistrationPeriod {
        get {
            List<SelectOption> listOfRegPeriod = new List<SelectOption>();
            for(S_Period__c certiPeriod : listPeriod) {
                listOfRegPeriod.add(new SelectOption( certiPeriod.Id, certiPeriod.Name));
            }
        return listOfRegPeriod;
        }
        set;
    }

I am fetching the periods (Quarters) from one of custom object and When submitted, the values for checkboxes are saved in another custom object

2
  • You'll need to save this data somewhere. Several possibilities include Custom Settings, or localStorage, or even Apex Cookies. It depends on how much space you need, and if you want it to be stored client-side or server-side. Do you know what you want to do?
    – sfdcfox
    Commented Jan 15, 2015 at 17:39
  • I am already storing the checkbox info to custom object as when I submit the page I am getting the ids of quarter which I am using it to perform another SOWL. The problem is I am not sure where shall I put the code to retrieve those values and set it to compare with previous one.
    – JavaAster
    Commented Jan 15, 2015 at 17:46

1 Answer 1

1

To load the data back into the page, you'd ordinarily do this in your constructor. Note that I'd restructure your code so it's more amenable to this design.

public String[] regPeriod { get; set; }
public SelectOption[] getRegistrationPeriod() {
    List<SelectOption> listOfRegPeriod = new List<SelectOption>();
    for(S_Period__c certiPeriod : listPeriod) {
        listOfRegPeriod.add(new SelectOption( certiPeriod.Id, certiPeriod.Name));
    }
    return listOfRegPeriod;
}
public Constructor() {
    regPeriod = new String[0];
    for(Custom_Object__c record:[SELECT Id, S_Period__c FROM Custom_Object__c WHERE OwnerId = :UserInfo.getUserId()]) {
        regPeriod.add(record.S_Period__c);;
     }
}

You'll need to tweak this code to match your class name, field names, etc.

3
  • why do you put String[0] in constructor.
    – JavaAster
    Commented Jan 15, 2015 at 18:16
  • Initialize variables inside constructors, when possible. This also allowed me to remove the getter/setter setup and use the default getter/setter methods {get;set;}. String[0] is similar to List<String>(), but I usually prefer shorter versions of code rather than longer versions. However, we can't write List<List<String>> as String[][], so in that case, I use List<String[]> instead.
    – sfdcfox
    Commented Jan 15, 2015 at 18:22
  • your comment is best answer to my question :)
    – JavaAster
    Commented Jan 15, 2015 at 18:34

You must log in to answer this question.

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