Convert String Field into Integer

_Samay
Tera Expert

Hi Friends, We have one custom table where we have string fields that we would like to convert into Integer fields. 

Please suggest , if this is possible. Because we are not getting the option to select type as integer or decimal for existing columns. 

1 ACCEPTED SOLUTION

Voona Rohila
Kilo Patron
Kilo Patron

Hi @_Samay 

We cannot change the datatype if there are any records with value for that field.

If this field is not yet moved to prod instance then you can clear out the data first and then you will be able change the type.

If the field is already in prod then It's better you create a new field, write a script to map the old field data to new field, update all the references of old field to new field and deactivate old field.

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

4 REPLIES 4

Vishal Birajdar
Giga Sage

Hi @_Samay 

 

If you are using script then you can use :

 

var string ="123";
var intV=parseInt(string,10);

 

 

VishalBirajdar_0-1696483478824.png

 

 

                                                                          OR

 

If you want user to put only numbers in string field then use regular expression on that field (client script)

 

        var fieldValue = g_form.getValue('your_field_name');

        var regex = /^[0-9]+$/;

        // Check regular expression matches
        if (!regex.test(fieldValue)) {
            
            alert('Please enter only numbers.');
            g_form.setValue('your_field_name', ''); // Clear the field
        }
    

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Voona Rohila
Kilo Patron
Kilo Patron

Hi @_Samay 

We cannot change the datatype if there are any records with value for that field.

If this field is not yet moved to prod instance then you can clear out the data first and then you will be able change the type.

If the field is already in prod then It's better you create a new field, write a script to map the old field data to new field, update all the references of old field to new field and deactivate old field.

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thanks Voona for your reply. 

Prince Arora
Tera Sage
Tera Sage

@_Samay 

 

short answer is NO

 

Long answer:

 

According to ServiceNow, if there's data in a table field, you can't change its data type directly.

To work around this:

You can perform form-level validation to accept only integer values and in the backend, you can use the "parseInt" function to convert it to an integer.

 

If you absolutely need it as a string, disable the old field and create a new one, but keep in mind you'll lose the existing data.

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.