[SalesForce] Controlling the data type of a custom field

Can I control the data type of a custom field?

I'd like to add a long or integer custom field to an object.

On the edit custom field screen I select Number as the Data Type as it seems like the only applicable custom field type. Currency and Percent wouldn't work.

On the Edit Custom Field screen I can alter the Length and Decimal Places custom field attributes. Setting the decimal places to 0 makes sense.

Length is a bit more tricky as I can't express the data type limits by the number of digits alone. E.g. integer spans -2,147,483,648 to 2,147,483,647, so at best I could had 9 digits before it would risk overflowing. For long 18 digits would take all but the most significant bit (so I could never actually store the maximum long value in a custom number field).

Salesforce Custom Field Length and Decimal Places

When working with the custom number fields in apex the data type is always decimal. So I need to convert to and from the required data type.

Via the partner API the soapType is xsddouble with the scale matching the decimal places and the precision the digit length.

Best Answer

The length and decimal places are only enforced when editing data via the standard web UI. Apex and API methods can actually save records with decimal places. This is true for standard and custom fields. To restrict the values to an integer, you'd have to put a validation rule in place or add a trigger to the object to do the validation.

EDIT: In the past I have seen numbers get inserted that don't match the field definition. I just tested using Apex, Workbench and Jitterbit to insert records and they all were rounded when viewing them in the web ui. I then added a trigger to capture the values before and after insert. The values have the decimals. Finally I inserted a record with decimals and then queried it using Apex and SOQL. The value is stored with the decimal! So, it looks like Salesforce has changed the display to match the definition, but they are stored in the database as inserted.