1

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.

4

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.

2
  • This is definitely not the case for all standard fields. For example, the following throws an error: Account acc = new Account(id='001J000000DXCHc',numberofemployees=12.3); Furthermore, the decimal portion of the number will not actually be saved to sf for custom fields. It will simply round that number to the precision on that field. – Greg Grinberg Oct 17 '12 at 0:11
  • That's really weird. I know for sure I've seen numbers that were updated via the api and they had decimal places even though the field was defined without them. I just tested an insert using Apex, workbench, and Jitterbit and they were rounded. I wonder if this has changed recently. – Daniel Hoechst Oct 17 '12 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.