Running background script question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2018 11:53 AM
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!
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2018 09:51 PM
Hi Kevin,
Any update on this?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2018 12:04 PM
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