1

Other developers before me made this field (text type) but it should be Number field. I can't change it now into number, because we will loose all records with this field... Is it possible somehow to set format for text field to be like as number: value="{0,number, $###,###,##0}"?

1
  • On VF page you can try this with apex:outputText or else you can create a new field and upload the data there and then delete old field. Dec 22, 2016 at 11:12

2 Answers 2

3

You could end up making this way more complicated than it really needs to be.

Let's look at the problem that you want to convert a Text field to a Number field. This isn't possible directly without losing all existing data which you've already identified. Why not consider creating a new number field and copying information from TextField1__c to NumberField1__c?

The way you'd do this is creating a new field: NumberField1__c and running a script that would copy the value from TextField1__c to NumberField1__c. You can use the Workbench and use Apex execute this script.

If you go to:

Utilities > Apex Execute

And use something similar to the following:

List<MyObject__c> objList = [SELECT TextField1__c, NumberField1__c FROM MyObject__c]; // Get all records from the object

for (MyObject__c obj : objList) { // Loop through the list
  obj.NumberField1__c = Integer.valueOf(obj.TextField1__c); // Set the number field to the integer value of the text field
}

Database.update(objList, false); // Update, setting AllOrNothing to false

After this, you'll be able to just delete the now redundant field. This is a bit more painful to do, but it will do more than just plaster over the problem by using formula fields to hide the fact the data is stored incorrectly.

Edit

After talking to you in the comments, you'll need to create a Formula Field with the return type of Number. From there, you'll need to use the formula:

VALUE(TextField1__c)

From the docs:

Description: Converts a text string to a number.

Use: VALUE(text) and replace text with the field or expression you want converted into a number.

9
  • hmm, ok. Let me try. Dec 22, 2016 at 11:17
  • @IgorStosic Be sure everything copies over correctly, only because you don't want to lose what's in the original text field. In may be worth using a List View or build a quick Report to check this before you delete the field.
    – Dan Jones
    Dec 22, 2016 at 11:26
  • Is there some other way to do this? Client told me to not do this way.. Dec 22, 2016 at 12:08
  • @IgorStosic Surely it's either a number field or it isn't?
    – Dan Jones
    Dec 22, 2016 at 12:22
  • I just need to change format into my visualforce page. So what i have: I have text field, but in that text field i have number into that text field *1000000, someone before me made mistake and set type of that field to be TEXT. Now i need to format that text field into 1.000.000$ but i can't format like that because it is text field... Dec 22, 2016 at 12:28
0

In support of @Dan Jones' answer, I'd like to point something out:

You cannot change a field type if there is a reference to that field in any code or automation, including deactivated items.

If you really want to change the field type, you need to delete all of the code that references it, make the change, then put all the code back.

Doing the above is literally twice as difficult as creating a new field of the correct type and changing your code to use that field.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .