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-01-2018 11:58 AM
I dont see any issue with the script. If you have a doubt try running it in a non-prod env. Also, you can set a Limit to 10 records, to test if it is updating properly.
updateDemands();
function updateDemands()
{
var gr = new GlideRecord('dmn_demand');
gr.setLimit(10);
//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();
}
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2018 11:58 AM
Hi Kevin,
yes running background script would be dangerous.
Best practices:
1) always run it for few records by using setLimit() during query
2) add log statements
3) use setWorkflow(false) to avoid triggering any business rules on that table
Sample script as per your requirement:
you can also put the date/time into GlideDateTime object and take date and then set it
updateDemands();
function updateDemands()
{
var gr = new GlideRecord('dmn_demand');
//gr.addQuery("number", 'DMND0002382');
gr.setLimit(5);
gr.query();
//gs.print('demand number: ' + gr.toString());
while(gr.next()) {
var gdt = new GlideDateTime(gr.u_end_date);
gr.u_end_date_demand = gdt.getDate();
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();
}
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
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:27 PM
I tried the code I entered before without your changes here in a sub-prod instance, and didn't receive any errors and the new Date "End Date" field had the correct field value there. Are you sure you can't convert directly from Date/Time to Date? I understand going from Date to a Date/Time due to lack of data, but I think Date/Time to Date makes sense.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2018 11:28 PM
Hi Kevin,
Yes you can directly set date from date/time or take date from date/time and set. both way it works.
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader