Converting a field from integer to percent

mikealderson
Tera Contributor

We defined a field as an integer.   It needs to be of type percent_complete.

We tried to change it but ServiceNow won't allow the change.

Worst case is to export existing values. Drop the column and create a new one and reimport the data.

Other option is to create another column Percent Complete as type Percent Complete and copy the existing values to that column.

That will require many changes to forms, reports, arrrgghhhh.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Mike,



There are some field types you cannot (and do not want to) convert from/to. This seems to be one of them. Doing so could endanger the data.



The safest approach is to create a new field and copy over the old one. Sorry about the extra work.



Let us know if you need help copying the old values to the new. A one time script in scripts background would do the trick. It would look something like this:



(function () {  




      var rec = new GlideRecord('YOUR_TABLE');    


      rec.query();  


 


      while (rec.next()) {  


            rec.u_percent = rec.getValue('u_integer'); // update field names accordingly


            rec.setWorkflow(false);   // don't trigger workflows or business rules


            rec.autoSysFields(false);   // don't update updated/update on/mod count


            rec.update();  


      }  


})();  


View solution in original post

4 REPLIES 4

Chuck Tomasi
Tera Patron

Hi Mike,



There are some field types you cannot (and do not want to) convert from/to. This seems to be one of them. Doing so could endanger the data.



The safest approach is to create a new field and copy over the old one. Sorry about the extra work.



Let us know if you need help copying the old values to the new. A one time script in scripts background would do the trick. It would look something like this:



(function () {  




      var rec = new GlideRecord('YOUR_TABLE');    


      rec.query();  


 


      while (rec.next()) {  


            rec.u_percent = rec.getValue('u_integer'); // update field names accordingly


            rec.setWorkflow(false);   // don't trigger workflows or business rules


            rec.autoSysFields(false);   // don't update updated/update on/mod count


            rec.update();  


      }  


})();  


If we create a new field called temp_complete and copy complete over to temp_complete can we then delete complete and then recreate complete in the new format and copy temp_complete over to complete?   What I am wondering is if all of the reports, lists, ACLs, etc. will reattach to the new field with the old name or if we will need to go in and recreate all that?


Hi Mike,



What I have found in many cases is that if you do this from the dictionary (not from form layout or form designer) reports and form layouts and all those other things remember where the old field name goes. I haven't done it in a while, so test.



Summary: Deleting a field from the dictionary and re-creating it USUALLY can be done transparently.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Mike,



Best practice : Instead of changing a field's type, create a new field with the correct type and migrate the data from the old field to the new field and, optionally, delete the original field.


Please refer section 3.1.4 for more info.


http://wiki.servicenow.com/index.php?title=Modifying_the_Application_Design#Change_Field_Type_with_C...