0

I have defined a Number field in a custom object with length 18 and 0 decimal places (Number(18, 0)). User can still enter a decimal value in page layout, but on saving the record, it removes anything after the decimal. So

  • if user enters 18.4, it's saved as 18.
  • if user enters 18.9, it's saved as 18.

I would like to throw an error if the user enters a decimal value. So I created a before update trigger and thought to throw an exception if I receive a decimal value. But when I tried to read the value in debug logs, I found the value to be an integer (in both the examples above, the value printed was 18). How can I solve this problem?

1
  • If this is a new field, you can change the field type to Number(17,1) and throw an error on before trigger Oct 9, 2021 at 3:52

1 Answer 1

1

This is a common discrepancy between understanding the purpose of the the decimal configuration and the data stored in the page and/or database. In lightning, you will notice rounding occur at the page level, but once you click the field (inline edit) again, you can see the decimal value. As a matter of fact, you can also run a SOQL to see the full value (with as many numbers after the decimal in SOQL) too.

Using a Validation Rule with the contains() function, you can restrict record updates when the Number data type field has a decimal / full stop.

CONTAINS(TEXT(NumberField__c), '.')

I'd place the error message at the field location to make it clearer.

You must log in to answer this question.

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