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
Viktor IliukhinViktor Iliukhin 

InputText integer "0" instead of "null"

SF developers,

I need an advice. I have visual page with inputText 
<apex:page controller="inputIntegerCtrl">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!saveValue}" reRender="bs"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" id="bs">
                <apex:inputText label="value" value="{!intValue}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
public class inputIntegerCtrl {
    public Integer intValue {get;set;}
    public void saveValue() {
    }
}
When I try to delete variable value and press button "Save", value integer variable is becoming "0" instead of "null"
I found a page that describes this problem https://salesforce.stackexchange.com/questions/64339/apex-inputtext-bound-to-integer-takes-0-instead-of-null

I try to create string setter 
public class inputIntegerCtrl {
    public Integer intValue;

    public String getIntValue() {
        return String.valueOf(intValue);
    }

    public void setIntValue(string intValue) {
        if (intValue != null)
            this.intValue = Integer.valueOf(intValue);
    }

    public void saveValue() {
    }
}

But, now, i have error message "
Error: Read only property 'inputIntegerCtrl.intValue'"

Does anyone know how to solve this problem?
Best Answer chosen by Viktor Iliukhin
DevADSDevADS
Viktor,

That was just an example to help you understand how you can use it. As I said, you need to use ternary operator to determine the text field filled with an interger value.

public String str {get;set;}
Integer value = !String.isBlank(str) ? Integer.valueOf(str) : null;

That's how you can get null in the 'Value' variable.

Do let me know if I am still missing on anything, would happy to help you.


 

All Answers

DevADSDevADS
Hi Viktor,

This behaviour is implicit on the platform. You can user ternary operator to determine if the field is blank, please see below example for a reference -
public class inputIntegerCtrl {
    public String intValue {get;set;}
    public void saveValue() {
        Test__c tstRec = new Test__c(Score__c = !String.isBlank(intValue) ? Long.valueOf(intValue) : null);
        Insert tstRec;
    }
}
NOTE:  Longs have a minimum value of -2^63 and a maximum value of 2^63-1

Happy Coding!! Do reply on the same post if you any follow up questions for the same thread.

 
Viktor IliukhinViktor Iliukhin
Hi DevADS,

Thanks for your answer, but I need field integer :-(
DevADSDevADS
This will work in case of integer field as well. 
Viktor IliukhinViktor Iliukhin
I'm sorry, but I think you don't understood me. I need intValue type integer, not integer filed sObject. I use integer variable intValue in inputText.
String type intValue, work wery well, but interger type has problem with "null"
 
DevADSDevADS
Viktor,

That was just an example to help you understand how you can use it. As I said, you need to use ternary operator to determine the text field filled with an interger value.

public String str {get;set;}
Integer value = !String.isBlank(str) ? Integer.valueOf(str) : null;

That's how you can get null in the 'Value' variable.

Do let me know if I am still missing on anything, would happy to help you.


 
This was selected as the best answer
Viktor IliukhinViktor Iliukhin
I have created additional setter and getter
public class inputIntegerCtrl {
    public integer intValue { set;get;}
    
    // for edit setter and getter
    public string getEdtIntValue (){
        return string.valueOf(intValue);
    }
    
    public void setEdtIntValue (string edtIntValue){
        this.intValue = (String.isEmpty(edtIntValue)) ? null : integer.valueOf(edtIntValue);
    }

    public void saveValue() {

    }
}
and use "virtual" variable edtIntValue for edit intValue  value 
<apex:page controller="inputIntegerCtrl">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!saveValue}" reRender="bs"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" id="bs">
                <apex:inputText label="value" value="{!edtIntValue}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

DevADS, thank you for help me.
DevADSDevADS
Great. Please mark this as  a "Best Answer" so that this thread can be closed. 

Happy Coding!!