Running background script question

Kevin McGartlan
Tera Contributor

Hi All, 

I'm fairly new to ServiceNow development, but I have been tasked with getting old data in a field to a newly created field on the same table. We want to copy the old "End Date" which is a Date/Time field, to the new "End Date" field which is a date field. I researched this and discovered that a background script would be the best option to get this done. However, I've also read that background scripts can be dangerous to run without the right syntax or logic in place to accomplish the right goal. Below is the script I plan to run:

updateDemands();

function updateDemands()
{
var gr = new GlideRecord('dmn_demand');
//gr.addQuery("number", 'DMND0002382');
gr.query();
//gs.print('demand number: ' + gr.toString());
while(gr.next()) {
   gr.u_end_date_demand = gr.u_end_date;
   gr.setWorkflow(false);
   gr.autoSysFields(false);
   gr.update();
   }
}

Is the syntax/logic here sound? I'm nervous running this as I don't want to lose the current data in there. 

Thank you! 

6 REPLIES 6

Hi Kevin,

Any update on this?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chalan B L
Giga Guru

updateDemands();

function updateDemands()
{try{
var gr = new GlideRecord('dmn_demand');
//gr.addQuery("number", 'INC0010051');

gr.query();
//gs.print('demand number: ' + gr.toString());
while(gr.next()) {
gs.print('dmn_demand due date::+gr.u_end_date)
gr.u_end_date_demand = gr.u_end_date;
gr.setWorkflow(false);
gr.autoSysFields(false);
//gr.update();
}
}catch(e){
gs.print('error'+e);
}

}

 

** before gr.update try to print inside the loop

** using try catch will save you sometimes

** if you are in london version then you can rollback the changes done[new feature]

 

Please mark the answer as correct and helpful if it helped you..!!

 

Regards.

Chalan