Background script to move text from one field to another

javis
Giga Expert

Hi all,

I need assistance on writing a background script to move all text from one field to another. I have a requirement to update all records to move the text from short description to description. I could update all 150 records manually or run a background script. Thoughts?

1 ACCEPTED SOLUTION

awessel
Kilo Guru

This should be fairly simple. Here you go.



var incRec = new GlideRecord('incident'); //This assumes you are talking about incidents. If not you can change incident to the correct table


incRec.addNotNullQuery('short_description'); //Only update those that have a value in short description


incRec.query();



while(incRec.next()) {


        incRec.description = incRec.short_description;


        //incRec.short_description = ''; //Uncomment if you want the short description blanked


        incRec.update();


}


View solution in original post

4 REPLIES 4

awessel
Kilo Guru

This should be fairly simple. Here you go.



var incRec = new GlideRecord('incident'); //This assumes you are talking about incidents. If not you can change incident to the correct table


incRec.addNotNullQuery('short_description'); //Only update those that have a value in short description


incRec.query();



while(incRec.next()) {


        incRec.description = incRec.short_description;


        //incRec.short_description = ''; //Uncomment if you want the short description blanked


        incRec.update();


}


Chuck Tomasi
Tera Patron

A background script would be quite easy. I recommend putting this in a scheduled job or fix script so you can test it in dev and move it to production.



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



(function () {  



      var rec = new GlideRecord('tablename');  


      rec.addQuery('field', value); // optional filter to get just the record syou want


      rec.query();  


 


      while (rec.next()) {  


              rec.description = rec.getValue('short_description');


              rec.update();  


      }  


})();  


Niklas Peterson
Mega Sage
Mega Sage

Hi,



You should also consider if you want the updates to run business rules and update the timestamps.



"setWorkflow" and "autoSysFields" methods can prevent that.




Regards,


Niklas


Arindam Ghosh
Mega Guru

You can use below script if you want to update all the records in the Table.


But if you have more than   record in the table but you want only 150 to be updated the you need to add one more addQuery with specific condition.



var gr = new GlideRecord("<Table Name>);


gr.addQuery("sys_id", '!=', '');


gr.query();


while(gr.next()) {


if(gr.short_description != '');


gr.description = gr.short_description;


gr.update();


}



Thanks,


Arindam