3

For a currency field (say 10,2) , Salesforce displays a standard error message below the field as

Error: Number is too large.

My requirement would be to display an apex page message instead.

I have added apex error message in Save method . But it's not working.

if( cc.Amount__c != null){
    Integer numOfDigits = cc.Amount__c == 0 ? 1 : (1 + (Integer)Math.floor(Math.log10(Math.abs(cc.Amount__c))));
                        system.debug('numOfDigits'+numOfDigits);
                            if(numOfDigits>12){
                                errorMain = true;
                                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.Error);
                                ApexPages.addmessage(myMsg);
                                return null;  
                            }
                        }
                        //End   

I could find other option as Try - Catch block. There is already a try catch for the entire set of operation . Within that , how can I insert this try catch ?

 try {

}catch(Exception ex){
             system.debug(ex);
            ApexPages.AddMessage(new   ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
        }

VF Page :

<apex:pageBlockSection columns="3" id="prd"  showHeader="true"  rendered="{!(Picklist value=='XXXXX')}">

                <apex:repeat value="{!AmountMap}" var="key" >               

                <apex:outputLabel value="Amount"/>
                <apex:outputPanel >
                <div class="requiredInput">
                     <div class="requiredBlock"></div>

                <apex:inputField value="{!AmountMap[key].Amount__c}" />
                </div>
                 </apex:outputPanel>
                </apex:pageBlockSectionItem> 

       </apex:pageBlockSection> 

Controller :

try {

 //Other operations
 if(AmountMap != null && !AmountMap.isEmpty()){
 //field assignments 
 }
 //other operations

 catch block

Here my condition will never satisfy if(AmountMap != null && !AmountMap.isEmpty()){ since error will be thrown by Salesforce beforehand.

3
  • 1
    You need to add a rerender in your markup. Since you haven't shared that, it is difficult to direct you on how to surface this error message on the front end.
    – Adrian Larson
    Feb 1, 2017 at 3:19
  • Hi Adrian - I have updated the Question . Feb 1, 2017 at 3:46
  • 1
    We need to see the save button.
    – Adrian Larson
    Feb 1, 2017 at 4:08

1 Answer 1

7

You can't override this in Apex Code directly. The reason why is because the error occurs before any Apex Code is called, or even if no Apex Code is called at all. You can verify that's the case by checking your debug logs.

Here's a demonstration:

<apex:page standardController="Account">
    <apex:form>
        <apex:pageMessages />
        <apex:inputField  value="{!Account.AnnualRevenue}" />
        <apex:commandButton value="Continue" />
    </apex:form>
</apex:page>

Example of Number is too large error

This feature is baked into the Visualforce engine, along with a lot of other automatic features, like making sure required fields are actually populated, etc.

If you want to provide a custom message, you'll have to either bind the value to a normal text field and parse it in Apex Code, or write JavaScript and parse the value before submission.

1
  • Ran into a similar issue . sfdcfox can you please provide an example for the binding into normal text field ? In this example , the value is kept in a map.
    – SFDCRookie
    Feb 3, 2017 at 12:56

You must log in to answer this question.

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