Find your content:

Search form

You are here

Number Field (18,0) taking default value as 0 inspite if i leave as empty?

 
Share
if(adlObj.Actual_Quantity_Received__c < 0 || adlObj.Actual_Quantity_Received__c==null)
        {
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter the Transaction Quantity equal to or greater than zero.'));
             return null;
        }

In this snippet Actual_Quantity_Received__c is a number field(18,0) and i am having issue that even i am leaving the field Actual_Quantity_Received__c as empty still its taking 0 and not entering if statement.

I checked its not that i have defaulted to 0.Any reasons why this is happening?


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #1

I do not recall the syntax off hand, but can you not simply alter the ..Received variable reference to be the "value()" of it and eval it against "0" at that point? Null, blank and zero would all qualify then, yes..?


Attribution to: AMM

Possible Suggestion/Solution #2

I suspect the issue here is related to the way that HTML inputs work. When you use an inputtext bound to a number field, there is an automatic conversion of the entered string to a numeric value. An empty number field needs to be null, but there's no way to submit a null into an HTML input - if you leave it blank it will go over the wire as an empty string rather than a null string.

According to the apex developer's guide, the string to numeric conversions should give the same behaviour as Java, but that would be to throw a NumberFormatException when trying to convert an empty string to a number, so my assumption has always been that this is a defensive technique to avoid these exceptions.


Attribution to: Bob Buzzard
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3439

My Block Status

My Block Content