1

I have an object field that is of number data type, based on a picklist value in the parent object field I want to render in the visual force page as an input check box. If the check box is checked true, I want the value saved to 1 and 0 if false. If the picklist value requires it to render as a text field and I want to save the value as a number field. I'm successful in rendering the field as inputcheckbox and inputfield, but its not saving the checkbox value to the field. It is saving the value if it is rendered as a text box.

<apex:datatable value="{!attendanceRows}" var="o" id="theTable"

<apex:inputField value="{!o[column01.FieldName]}"  rendered="{!IF(c.Type__c='High School',true,false) }"/> 
<apex:inputCheckbox value="{!o[column01.FieldName]}"  rendered="{!IF(c.Type__c='Middle School',true,false) }"/>

In my controller I'm doing an upsert to the object.

public List<School__c> attendanceRows { get; set; }

public void SaveAttendance() {
    try {
        if (attendanceRows.size() > 0) {
            upsert attendanceRows;
        }
    }
    catch(DmlException e) {
        System.debug('An unexpected error has occurred while saving to School__c: ' + e.getMessage());
    }

}

// Header Definition Class
public with sharing class HeaderDefinition
{
    public HeaderDefinition(Boolean pIsVisible, String pColumnName, String pFieldName)
    {
        isVisible = pIsVisible;
        ColumnName = pColumnName;
        FieldName = pFieldName;
    }

    public Boolean isVisible { get; set; }
    public String ColumnName { get; set; }
    public String FieldName { get; set; }

    public string outString()
    {
        return FieldName + ' :: ' + ColumnName + ' - ' + String.valueOf(isVisible);
    }
}

SaveAttendance();

How do I save/get the value of the inputcheckbox?

  • Can you please include the relevant code from your controller? If you are able to provide any debug details as well, it will be also appreciated. thanks. – Eduard Jul 19 '18 at 8:32
  • Thanks Eduard, I added the relevant code from my controller. The debugs logs for the specific column is showing as null. Its not getting the value from the checkbox – Donald Reagan Jul 19 '18 at 23:22
0

You problem is that you're trying to assign a value of a Number fields to a checkbox which apparently does not work. Just see an example:

Visualforce:

<apex:page controller="testController">
    <apex:form id="testForm">
        <apex:pageBlock >
            <apex:pageBlockTable value="{! accounts }" var="acc" style="table">

                <apex:column value="{! acc.Id }"/>
                <!--
                <apex:column headerValue="Number of locations Checkbox" >
                    <apex:inputCheckbox value="{! acc.NumberofLocations__c }" ></apex:inputCheckbox>
                </apex:column>
                -->
                <apex:column headerValue="Number of locations" >
                    <apex:inputField value="{! acc.NumberofLocations__c }" ></apex:inputField>
                </apex:column>

            </apex:pageBlockTable>
            <apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>
        </apex:pageBlock>

        <apex:commandButton value="Update" action="{! updateAccs }" rerender="testForm" />

    </apex:form>
</apex:page>

Controller:

public class testController
{
    public List<Account> accounts {get; set;}

    public testController()
    {
        accounts = [SELECT Id, Name, CreatedDate, NumberofLocations__c  FROM Account WHERE ID ='0011H00001QOYcs'];
    }

    public void updateAccs()
    {
        System.debug('accounts=' + accounts);
        upsert accounts;
    }
}

Inspect the debug when updateAccs() has run:

13:09:42:010 USER_DEBUG [12]|DEBUG|accounts=(Account:{Id=0011H00001QOYcsQAH, Name=Dickenson plc, CreatedDate=2018-06-13 09:15:06, NumberofLocations__c=null, CurrencyIsoCode=USD})

Now uncomment the block of code on the page and inspect the debug againg:

-N/A

It's empty, because updateAccs() never gets executed. Checkout this Why is action not executed in controller? question for more details. If you added <apex:pageMessages/> to your page you would see an error message relating to assigning a Boolean to a number field.

Value 'false' cannot be converted from Boolean to Number

Nevertheless, I can suggest you the following solution. You would need two custom fields on your child object: a checkbox and formula. The checkbox is simple - use it with <apex:inputCheckbox value="{!o[column01.FieldName]}" rendered="{!IF(c.Type__c='Middle School',true,false) }"/> in your page. The formula field of a Number type would be a final result from your checkbox and number field and the actual formula should be something like:

IF( ISBLANK( NumberofLocations__c ), IF( NumberofLocationsCheckBox__c, 1, 0), NumberofLocations__c )

If your Number field is empty, which means your users didn't provide any value to that field the Formula will return 0 or 1 depending on the checkbox value.

Of course, there are other options to solve your problem, e.g. do this logic in controller before performing upsert.

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.