Setting Default value for old records

balaji_charapal
Kilo Guru

Hi,

I have created new field "Upgrade Version" of type choice list(example: Geneva, Helsinki, Istanbul ) and Default value(Helsinki) with --None--.

The problem is for (Historical Data)old records also it is setting Helsinki as a value, we don't want to sent value for old records.

How can we achieve this? or Is it a ServiceNow Behaviour?

1 ACCEPTED SOLUTION

That's correct. Your update set is going to contain the latest changes to the dictionary entry and that's what's getting applied to test and prod (effectively negating the two step process I said before.)



If you cannot or don't want to do it that way, then a Fix Script is needed to "null out" the existing records immediately after applying the update set so only new records have the new default value.



That script would look something like this:



Standard disclaimer: The following code is untested, requires review and potential modifications.



(function () {  



      var rec = new GlideRecord('your_table');   //change your_table to your table name



      rec.query();  


 


      while (rec.next()) {  


                  rec.setValue('your_field', ''); // change your_field to your field


                  rec.autoSysFields(false); // do not update mod_count, updated time, etc.


                  rec.setWorflow(false); // do not run business rules or workflows


                  rec.update();  


      }  


})();  


View solution in original post

4 REPLIES 4

Chuck Tomasi
Tera Patron

Hi,



Create the field with no default value, then once the field is created, go back and set the default value. Existing records will have a value of none and new records will get the new default value as they are created.


balaji_charapal
Kilo Guru

Thanks for your response Tomasi,



It is working as expected in Dev, when i move the code to QA then only it is happening like this.


You'll need to do the two updates Chuck described in two separate update sets.   In the same update set, it is only one record thus it has a default on-creation


That's correct. Your update set is going to contain the latest changes to the dictionary entry and that's what's getting applied to test and prod (effectively negating the two step process I said before.)



If you cannot or don't want to do it that way, then a Fix Script is needed to "null out" the existing records immediately after applying the update set so only new records have the new default value.



That script would look something like this:



Standard disclaimer: The following code is untested, requires review and potential modifications.



(function () {  



      var rec = new GlideRecord('your_table');   //change your_table to your table name



      rec.query();  


 


      while (rec.next()) {  


                  rec.setValue('your_field', ''); // change your_field to your field


                  rec.autoSysFields(false); // do not update mod_count, updated time, etc.


                  rec.setWorflow(false); // do not run business rules or workflows


                  rec.update();  


      }  


})();