How to change a data field to float field

Hey guys,
What are the possibilities to change an existing data field into fold without loosing the previously added data ?

Thank you in advance for the help.

What do you mean by fold field?

@Iulian_Olaru, Field Type cannot be Fold.

You can add Section Break field before the Data field and set Hidden attribute on the Data field if you don’t need it to be shown.

I’m sorry for the confusion, what I meant was to change a data field into a float field for entering numbers.

My intention is to use the Show Total function inside the Report Builder.

Unfortunately, when trying to change a data field to float I get this error: Fieldtype cannot be changed from Data to Float in row 85

Is there a workaround ?

Hi @Iulian_Olaru,
I think you are using customization.
Try to change from doctype list.

@Saditi thank you for your reply.

Yes indeed from Customisation I am doing it because it’s a custom data field.

In the doctype list I can’t see the custom fields.

Any idea ?

@Iulian_Olaru, try to add these fields in doctype it will allow you to change datatype more easily.
Using this, you can later change datatype through customization also.

Ok that’s a good point.

But my Event doctype already contains thousands of records… if I add the same custom field in the doctype, will I loose the data previously added ?

@Iulian_Olaru Please be aware, that if you alter core DocTypes, it’s not guaranteed to work after future ERPNext updates.

Usual pattern is to add a new field without changing the existing one, then migrate data from the old one to the new one using import tool or raw SQL. Then you can either drop the old field and rename the new one or point your scripts to the new field instead of old one.

Wow that becomes complicated … so there is no safe way to convert a data field to a field allowing number calculations inside erpnext ?

And here’s a usability question: why the hell would you name a field that you can enter numbers “float” ?

Why not name it easier for anyone to understand what to do with it “numbers” or anything related to numbers.

If I new from the beginning I would have used that field.

Hi @Iulian_Olaru,

there are different types of numbers, that is why you can choose from Int, Float, … that should come easy once you have seen it.

As for the customisation: for integrity reasons, the system will prevent type changes. If you are absolutely sure what you are doing, you can delete your custom field, update, then add the field with the same name but type float again and update. The database will maintain the column and keep your content… Make sure to make a backup before you do this.

@lasalesi So what you are saying is that if I delete the data field name “test” and then I add the same field name “test” but as Int field to be just a simple number… the data will be preserved ?

Because we have thousands of previous events added using the data field … we cannot loose that data!

Assume you have a field total of type Data and you want a field total of type Float. The safe way would be this:

  1. Add a new field total_float.
  2. Get all data from total
  3. Convert the data to floating point numbers by some custom logic.
  4. Write it into total_float
  5. See if it is present in ERPNext and looks as you expect.
  6. Delete the field total
  7. Rename total_float to total

Steps 2. - 4. with python:

doc_list = frappe.get_list("My DocType")

for name in doc_list:
    doc = frappe.get_doc("My DocType", name)
    doc.total_float = float(doc.total)
    doc.save()
1 Like

I managed to find the solution and I think it’s supposed to be known by everyone although it should be public and user friendly written in the user manual… but hey, who knows why some people do what they do.

Solution:

Let’s say you have a Customized Field named “Label”:“Age”, it’s data-type is “Type”:“Int” and “Name”:“age” [As stated in Customize Form Layout]

You want to change the data-type to float and the data has to be preserved.

You need to delete the Custom Field named “Age” and then Update the Customize Form. Now,

Add the same name field in the Customize Form “Label”:“Age” , data-type “Type”:“Float” and “Name”:“age” [Caution :- Make Sure the “Name”:“age” should be same]

I tested this information as I received it from someone who I paid in this community … it looks like a piece of information copied from somewhere else… but it works.

This might work with some changes, but not with others. The types Int and Float are pretty similar, so it’s not a big problem to convert between them. (3 -> 3.0)

Glad it worked for you, but switching from Data to Float is more difficult because a Data field can contain strings like “Hello World” that are more difficult to represent as a number. (“Hello World” -> ??)

What you’re doing works because ERPNext doesn’t delete the underlying data when you remove a field and your data fields probably contained only numbers anyways.