3

I have the following Visual force page:

    <apex:form id="mainForm" >
    <apex:actionFunction name="renderConsolePage" action="{!renderConsolePage}" rerender="out1"/>

    <apex:pageMessages id="showmsg"/>
    <apex:outputPanel id="out1">
        <apex:outputPanel id="consoleOutput">
            <apex:pageblock id="pbConsole" mode="inlineEdit">
                <apex:pageblockbuttons >
                    <apex:commandbutton title="{!$Label.Save_Button_Label}"
                                        action="{!saveCustomObject}"
                                        rerender="mainForm, showmsg"
                                        value="{!$Label.Save_Button_Label}" />
                </apex:pageblockbuttons>
                <apex:pageblocksection id="pbsConsole" columns="2">
                    <apex:repeat value="{!ListOfFields}" var="fld">
                        <apex:inputfield required="{!fieldMap [fld]}" value="{!customObject[fld]}" />
                    </apex:repeat>
                </apex:pageblocksection>
            </apex:pageblock>
        </apex:outputPanel>
    </apex:outputPanel>
</apex:form>

This is a Dynamic page that renders input fields for a Custom Object, these fields can be Text, DateTime, Picklists, checkboxes etc.

The ListOfFields is a List of the API names that are referenced in the customObject which is the custom Object that has been retrieved using a regular dynamic SOSL query.

What this page does is present the input fields and allow for the user to enter the values and save the custom object (it also updates in the same manner). All the fields are shown as expected and saving works as expected.

My issue is that for the Checkboxes I can see that the values are saved in the custom object (when I inspect this on the object level), but when I do a page refresh or simply log out and return, the checkboxes are blank (even if they should be checked).

All the other values for Text fields, lookups, picklists etc. are shown as expected but for checkboxes they are unchecked, regardless of the value that was entered before clicking save.

I have tried to only reference one field (a checkbox) and instead of having apex:inputField, using apex:inputCheckbox, but still the same abscence of the correct value (even if I can see that before the page loads (the query) holds the correct value of "true" and that it is shown as correct on the custom object record.

I have no idea if this is something really simple I am missing or similar, I have not found any known issues regarding this as well and would appreciate any help.

Ciao!

  • I think you should double check your SOQL query, how checkbox values are read. Maybe the are corrupt before you display it on the page... – Sergej Utko Apr 20 '16 at 8:34
  • Thanks for the reply, I have a debug set after the query and it returns the following: Custom_Object__c:{Id=XXXXX, Name=YYYYY, Custom_Checkbox_1__c=true, Custom_Checkbox_2__c=true, RecordTypeId=ZZZZZZZ} for the example when I have two checkboxes that are both checked on the Custom Object. – erikduvet Apr 20 '16 at 8:40
  • 1
    Could you please provide you controller code snippets, specifically what are ListOfFields and fieldMap variables. – edelrabe Apr 22 '16 at 11:35
  • ListOfFields is simply a String List that holds API names for the fields that should be added to the page: {CustomField1__c,CustomField2__c,...,CustomFieldN__c}. fieldMap holds the API name as key value and a Boolean value as value to decide if the field is required or not: {(CustomField1__c,true),(CustomField2__c,false)}, indicates that the CustomField1__c field is required. – erikduvet Apr 22 '16 at 12:40
  • Yes, please show us the controller code. – Jagular Apr 22 '16 at 18:50
1

Since you didn't provide class, I've copypasted your page and created some dummy controller. Everything works great. If it's not workflow or trigger, may be you have some browser issues?

Please try this:

public class DynamicControllerSimple {

public map <string, boolean> fieldMap{get; private set;}
public list <string> listOfFields{get; private set;}
public SObject customObject {get;set;}

public DynamicControllerSimple() {
    String qid = ApexPages.currentPage().getParameters().get('id');

    customObject = new opportunity();
    String objType = customObject.getSObjectType().getDescribe().getName();

    Map<String, Schema.SobjectField> fields = 
        customObject.getSObjectType().getDescribe().fields.getMap();

    fieldMap = new map <string, boolean>();
    listOfFields = new list<string>();

    string query ='';
    for(String s :fields.keySet()){
        Schema.DescribeFieldResult field = fields.get(s).getDescribe();
        fieldMap.put(s,!field.isNillable());//should be false for BOOLEAN
        listOfFields.add(s);
        query += s +',';
    }
    query = query.subString(0,query.length() - 1);

    try {
        customObject = Database.query('select '+ query +' from ' + objType + ' where id = :qid');
    } catch (exception e) {}
}
public void saveCustomObject() {
    upsert customObject;
}
public void renderConsolePage(){}
}

You can you use it with or without id:

/apex/dynamicControllerS?id=00636000006FRAt

Dynamic layout for Opportunity

  • Thanks for your reply, how does your visual force page look then? Since it appears to work fine with the controller code, before the page renders. But then the chekboxes looses there value in the page (still checked correctly on original Object). – erikduvet Apr 25 '16 at 12:01
  • I'm using your original page, just added <apex:page controller="DynamicControllerSimple" > Does controller I've provided works for you? Try customObject = new yourObjName(); Could you share with your controller class? – Ilya Lepesh Apr 26 '16 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.