function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jneilan22jneilan22 

Enter Zero in VF Page

Hello,

 

I have a VF page with a custom controller that works great when entering values into the form fields.  However, I also need to allow users the option of removing values they have previously input.  I've worked with SFDC premier support on the code below but I have not been able to figure out how to allow a zero to be entered and replace an existing value.  In the custom controller, there is an expression to validate that the variable in the VF form is not zero because the value apparently defaults to zero.  However, this is also preventing someone from entering zero in order to remove a previous value.  Does anyone have any ideas?

 

The VF Page:

 

<apex:page standardController="Population_Assumption__c" tabStyle="Population_Assumption__c" extensions="CustomOppController"
 sidebar="False" Title="Update Population Assumptions" recordSetVar="opps" id="mupop">
    <apex:messages />
    <apex:form id="muform">
    <style type="text/css">
    .bPageBlock.pbBody { background-color:#0000CD; }
    .format { font-style:italic; font-weight:bold; color:#0000CD; }
    .box { background-color:LightSteelBlue; }
    .button { text-align:center; padding-top:4px; }
    </style>

        <apex:pageBlock title="UPDATE POPULATION ASSUMPTIONS" mode="edit" id="mub1">
             <apex:pageBlockSection columns="2">
             <apex:outputLabel for="muselectedlist" styleClass="format" value="Eligible Employee Population: ">
                <apex:inputText styleClass="box" value="{!NewEEVal}" id="muselectedlist">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

             <apex:outputLabel for="SpousePop" styleClass="format" value="Eligible Spouse Population: ">
                <apex:inputText styleClass="box" value="{!NewSPVal}" id="SpousePop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

            <apex:outputLabel for="DepPop" styleClass="format" value="Eligible Dependents Population: ">
                <apex:inputText styleClass="box" value="{!NewDEPVal}" id="DepPop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

            <apex:outputLabel for="SeniorPop" styleClass="format" value="Eligible Seniors Population: ">
                <apex:inputText styleClass="box" value="{!NewSENVal}" id="SeniorPop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

            <apex:outputLabel for="OtherPop" styleClass="format" value="Eligible Other Population: ">
                <apex:inputText styleClass="box" value="{!NewOTHVal}" id="OtherPop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR><BR></BR><BR></BR>

             </apex:pageBlockSection>
            <apex:pageBlockButtons location="top">
                <apex:commandButton styleClass="button" style="background:DarkSeaGreen" value="Save" action="{!Ab}"/>
                <apex:commandButton styleClass="button" style="background:DarkSeaGreen" value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>

         <apex:pageBlockTable value="{!opps}" var="p">
            <apex:column value="{!p.Name}"/>
            <apex:column value="{!p.EE_Eligible_Population__c}"/>
            <apex:column value="{!p.Spouse_Eligible_Population__c}"/>
            <apex:column value="{!p.Dependents_Eligible_Population__c}"/>
            <apex:column value="{!p.Seniors_Eligible_Population__c}"/>
            <apex:column value="{!p.Other_Eligible_Population__c}"/>

        </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

The Custom Controller:

 

public class CustomOppController{

public CustomOppController(ApexPages.StandardSetController controller) {
oppid=System.currentPageReference().getParameters().get('id');
    }

public List<Population_Assumption__c> PopulationInfo{get;set;}

public String oppid{get; set;}
public Integer NewEEVal{get;set;}
Public Integer NewSPVal{get;set;}
Public Integer NewDEPVal{get;set;}
Public Integer NewSENVal{get;set;}
Public Integer NewOTHVal{get;set;}

public CustomOppController()
{
}

public PageReference Ab()
{       
system.debug('####################');   
PopulationInfo=new List<Population_Assumption__c>();
   for(Population_Assumption__c pop :[Select EE_Eligible_Population__c,Spouse_Eligible_Population__c,Dependents_Eligible_Population__c,
   Seniors_Eligible_Population__c,Other_Eligible_Population__c
   FROM Population_Assumption__c
   WHERE Opportunity__c=:oppid])
    {
     system.debug('po####p:'+pop);
     system.debug('@@@@@@@@ NewEEVal :'+NewEEVal );
     system.debug('######## NewSPVal:'+NewSPVal);
     system.debug('$$$$$$$$ NewDEPVal:'+NewDEPVal);
     system.debug('%%%%%%%% NewSENVal:'+NewSENVal);
     system.debug('^^^^^^^^ NewOTHVal:'+NewOTHVal);
     system.debug('@@@@@@@@ pop.EE_Eligible_Population__c:'+pop.EE_Eligible_Population__c);
     system.debug('######## pop.Spouse_Eligible_Population__c :'+pop.Spouse_Eligible_Population__c );
     system.debug('$$$$$$$$ pop.Dependents_Eligible_Population__c:'+pop.Dependents_Eligible_Population__c);
     system.debug('%%%%%%%% pop.Seniors_Eligible_Population__c:'+pop.Seniors_Eligible_Population__c);
     system.debug('^^^^^^^^ pop.Other_Eligible_Population__c:'+pop.Other_Eligible_Population__c);
    
      IF ( NewEEVal!= null && pop.EE_Eligible_Population__c!= NewEEVal && NewEEVal!=0) {
          pop.EE_Eligible_Population__c = NewEEVal;
      }
      IF ( NewSPVal!= null && pop.Spouse_Eligible_Population__c != NewSPVal && NewSPVal!=0) {
          pop.Spouse_Eligible_Population__c = NewSPVal;
      }
      IF (NewDEPVal != null && pop.Dependents_Eligible_Population__c != NewDEPVal && NewDEPVal!=0 ) {
          pop.Dependents_Eligible_Population__c =  NewDEPVal ;
      }
      IF (NewSENVal != null && pop.Seniors_Eligible_Population__c != NewSENVal && NewSENVal!=0) {
          pop.Seniors_Eligible_Population__c =  NewSENVal ;
      }
      IF (NewOTHVal != null && pop.Other_Eligible_Population__c != NewOTHVal && NewOTHVal!=0) {
          pop.Other_Eligible_Population__c = NewOTHVal;
      }   
    PopulationInfo.add(pop);
    }

Try {
    update PopulationInfo;
    } catch(DMLException e) {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please click Cancel');
    ApexPages.addMessage(myMsg);
}  

PageReference Newpage=new PageReference('/'+oppid);
return NewPage;
}
}

 

bob_buzzardbob_buzzard

Could you allow the delete the contents of the field, and thus send back a blank value?

jneilan22jneilan22

I really don't mind how it is done, but I need to let the users change the field from a value to either blank or zero.  One other caveat is that there can be multiple records that populate with the same value.  So the custom object I created (Population Assumption) can have 5 population values input through the VF form.  There can be multiple Population Assumption records, but they all have the same values for the 5 variables.  I'm certainly willing to allow them to delete the contents of one of the variables, I'm just not sure how to do it in the code I have now.

bob_buzzardbob_buzzard

If you delete the contents of a field, what does the integer value come back as?  If its null, then you just need to check if there was a value there before and if there was, allow the submission. 

jneilan22jneilan22

The value comes back as zero, but it also deafualts to zero so if you leave a field empty, it will overwrite the value currently in the field if I do not include the !=0 statements in the controller.

bob_buzzardbob_buzzard

In that case I think you'll need to change your properties to Strings and check if they are empty.  That way you'll be able to distnguish between zero and deleted.  Then you can convert the value to an integer using the ValueOf method.

jneilan22jneilan22

Thanks for the tip.  I tried converting the variables to string and them comparing the results using integer.valueOf(variable), but it throws me an error "System.TypeException: Invalid integer:"  Any idea why this would happen?

bob_buzzardbob_buzzard

Are you checking if the string is null or empty before executing the Integer.valueOf method?