- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 08:22 AM
Hello - i have created a new filed - which is a Decimal Type.
I have a requirement for that field to show up to only 5 decimals. And so i made max length = 5. However, when i save the record and it refreshes, it sets it to 15.
What can i do so that field is populated w/ a number only up to 5 decimals?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 04:39 PM - edited 11-01-2023 04:39 PM
There is a workaround given in KB0696658, did you try those steps.
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 08:32 AM
Hi @Chuckie ,
In ServiceNow, the `decimal` field type doesn't have a built-in property to specify the number of decimal places. It allows up to 15 decimal places by default. If you want to restrict the number of decimal places to 5, you can use a `string` field type and then apply validation using a Business Rule.
Here's how you can achieve this:
### 1. Change Field Type:
Change the field type from `decimal` to `string`.
### 2. Create a Business Rule:
Create a Business Rule to validate and restrict the number of decimal places entered by the user. Here's an example of the Business Rule script:
(function executeRule(current, previous /*null when async*/) {
var decimalField = current.decimal_field.toString(); // Convert the field value to a string
var decimalParts = decimalField.split('.');
if (decimalParts.length > 1 && decimalParts[1].length > 5) {
// More than 5 decimal places, set an error and revert the field to the previous value
gs.addErrorMessage("Decimal field can have up to 5 decimal places.");
current.decimal_field = previous.decimal_field; // Revert to the previous value
}
})(current, previous);
In this script:
- `decimal_field` is the name of your string field.
- The script checks if the field has more than 5 decimal places. If it does, it sets an error message and reverts the field to its previous value.
Please adjust the field name `decimal_field` in the script according to the actual field name you're using.
Remember to test the Business Rule thoroughly in a non-production environment to ensure it behaves as expected before deploying it to your live instance.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 09:08 AM
Hi @Chuckie ,
You can write a regular express to check required decimal place or go with as suggested by @Danish Bhairag2 .
RegEx : ^\$([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$
Example with 2 decimal place
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 03:52 PM
Ashish - i am on the dictionary form for this field. The field type = decimal.
But how do i add a regular expression to it? is that allowed on a decimal type field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 04:16 PM
Ashish - i also try link below - but after i set scale=5 in the attributes field, i then update max length to 5 and save. After i save it refreshes back to 15. I can't seem to change max length to 5.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0696658