1

I currently have a radio select that when the user selects a value re-renders an apex:pageblocksection(Usage Estimator) to enable some input fields in it (based on the screenshot below).

enter image description here

The problem is when I've selected a value for each radio select field, it renders the section and puts a defualt "0" value on the other fields on that section as per screenshot.

Steps:

  1. Select a Gas value,which renders the section to enable Gas row fields(set of fields screenshot above)
  2. Select an Electricity value, which renders the section to enable Electricity row fields(set of fields screenshot above),but then changes the values of the gas row fields with "0"

enter image description here

Am I doing something wrong with rerendering the section?

Here are the snippets from the visualforce page and extension class:

VF Page

<!--ELECTRICITY Radio Button-->
<apex:selectRadio value="{!selectedElecICP}" layout="pageDirection" rendered="{!elecICPList.size > 0}">
<apex:actionsupport action="{!searchICPElecServicesDetails}" event="onchange" 
                    reRender="icpelecdetailpanel,econs,estimators"
                    status="waitStatus"/>
<apex:selectOptions value="{!ElecItems}"/>

<!--GAS Radio Button-->
<apex:selectRadio value="{!selectedGASICP}" layout="pageDirection" rendered="{!gasICPList.size > 0}">
<apex:actionsupport action="{!searchICPGasServicesDetails}" event="onchange" 
                    reRender="gasdetailpanel,gcons,estimators"
                    status="waitStatus"/>
<apex:selectOptions value="{!GasItems}"/>

<!--Usage Estimators pageblocksection-->
<apex:pageBlockSection title="Usage Estimator" columns="4" id="estimators">
<apex:selectList size="1" id="estBy" value="{!estimateBy}" label="Estimate By" disabled="{!IF(selectedElecICPDetail==null && selectedGasICPDetail==null, true ,false)}">
    <!--apex:selectOption itemValue="EAC" itemLabel="per Annum"/-->
    <apex:selectOption itemValue="Period" itemLabel="per Period"/>
    <apex:selectOption itemValue="Questions" itemLabel="by Questions"/>
    <apex:actionsupport event="onchange" reRender="estimators" action="{!resetUsageCalculation}"/>
</apex:selectList>
<apex:inputCheckbox label="Override" >
</apex:inputCheckbox>
<apex:outputText ></apex:outputText>
<apex:outputText ></apex:outputText>

<!--Electricity-->
<apex:inputText  label="day/s" disabled="{!IF(selectedElecICPDetail==null,true,false)}" id="numDaysE" value="{!numberOfDaysE}"/>
<apex:inputText  label="kWh" disabled="{!IF(selectedElecICPDetail==null,true,false)}" id="consE" value="{!totalConsumptionE}"/>
<apex:inputText  label="Electricity" value="{!elecConsumption}" id="econs" disabled="{!IF(selectedElecICPDetail==null,true,false)}"  html-placeholder="kWh"/> 
<apex:pageBlockSectionItem>
    <apex:image value="{!elecUsageImgURL}" rendered="{!elecConsumption != null}" height="25px" width="25px"/>
</apex:pageBlockSectionItem>

    <!--Gas-->
<apex:inputText label="day/s" disabled="{!IF(selectedGasICPDetail==null,true,false)}" id="numDaysG" value="{!numberOfDaysG}" />
<apex:inputText   label="kWh" disabled="{!IF(selectedGasICPDetail==null,true,false)}" id="consG" value="{!totalConsumptionG}"  styleClass="slds-input"/>
<apex:inputText  label="Gas" value="{!gasConsumption}" id="gcons" disabled="{!IF(selectedGasICPDetail==null,true,false)}" html-placeholder="kWh"/>
<apex:pageBlockSectionItem>
    <apex:image value="{!gasUsageImgURL}" rendered="{!gasConsumption != null}" height="35px" width="35px"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>

Extension class

 //Consumption variables
public decimal elecConsumption {get; set;}
public decimal gasConsumption {get; set;}
public decimal lpgConsumption {get; set;}

//Estimate by Period variables
public integer numberOfDaysE {get; set;}
public integer numberOfDaysG {get; set;}
public integer numberOfDaysL {get; set;}
public decimal totalConsumptionE {get; set;}
public decimal totalConsumptionG {get; set;}
public decimal totalConsumptionL {get; set;}

    public PageReference searchICPElecServicesDetails(){ 
    elecConsumption = null;
    if(selectedElecICP != 'None') {
        //selectedElecICPDetail = mapICPElecSummary.get(selectedElecICP);
        SearchICPRegistryService.ICP_element icp = new SearchICPRegistryService.ICP_element();
        icp.type_x = 'ELECTRICITY';
        icp.ICPIdentifier = selectedElecICP;

        SearchICPRegistryService.icpDetail_element icpDetail = new SearchICPRegistryService.icpDetail_element();
        icpDetail.searchOtherRegistry = false;
        icpDetail.ICP = icp;

        SearchICPRegistryService.searchICPRegistryRequest_element regReq = new SearchICPRegistryService.searchICPRegistryRequest_element();
        regReq.icpDetail = icpDetail;


        showICPElectricSummary = true ;
        selectedElecICPDetail = searchICPDetailRegistryService(regReq,'ELECTRICITY');   

    } else {
        showICPElectricSummary = false;
        selectedElecICPDetail = null;
        totalConsumptionE = null;
        numberOfDaysE = null;
    }
    return null ;
}

public PageReference searchICPGasServicesDetails(){ 
    gasConsumption = null;
    if(selectedGASICP != 'None') {
        SearchICPRegistryService.ICP_element icp = new SearchICPRegistryService.ICP_element();
        icp.type_x = 'GAS';
        icp.ICPIdentifier = selectedGASICP;

        SearchICPRegistryService.icpDetail_element icpDetail = new SearchICPRegistryService.icpDetail_element();
        icpDetail.searchOtherRegistry = false;
        icpDetail.ICP = icp;

        SearchICPRegistryService.searchICPRegistryRequest_element regReq = new SearchICPRegistryService.searchICPRegistryRequest_element();
        regReq.icpDetail = icpDetail;

        showICPGasSummary = true;
        selectedGasICPDetail = searchICPDetailRegistryService(regReq,'GAS');    
    } else {
        showICPGasSummary = false;
        selectedGasICPDetail = null;
        totalConsumptionG = null;
        numberOfDaysG = null;
    }
    return null ;
}

1 Answer 1

3

This is related to using <apex:inputText> for merge fields like {!numberOfDaysG}

When the AJAX event is invoked, and the controller setters for variables of type Integer (e.g. numberOfDaysG) are set and then the getters are invoked, a null value sent from {!numberOfDaysG} to the controller is auto-converted in the setter to a 0 value - even though on the form it is null!

There are a variety of ways to work around this:

  • Use setter variables of type String (and, convert within the controller to Integer before you need to do calculations or DML) or ...
  • Use proxy SObjects fields of type Number (like Account.NumEmployees) and Visualforce field markup using apex:inputField - null values will stick as null in the controller variable.
3
  • Using apex:inputField and binding it to a Account object like you said might cause issues if the running user doesn't have access to account object.
    – javanoob
    Commented Nov 2, 2016 at 0:41
  • 2
    @javanoob -- true; proxy objects won't work in all use cases - but for OP they offer an option - since OP did a good job of presenting only the relevant sections of the problem, I don't know if there's a domain object that can suffice
    – cropredy
    Commented Nov 2, 2016 at 0:45
  • Thanks @cropredy, didn't know that AJAX request automatically converts null integers to 0 when its invoked. I can't use type String since I'm actually looking at using <apex:input> so that users won't be able to input alphabet characters in the fields. Another work-around that I found out is to rerender using field specific ids instead of rerendering the whole section. Thanks for the info :)
    – sfdx
    Commented Nov 2, 2016 at 1:12

You must log in to answer this question.

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