- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi All,
I have a requirement where I need to fetch only Dates and not time from the OOB field 'Due Date' on SCTASK and update it on newly created Due Date field which is of type date. Please help me in writing the script to achieve this.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
something like this
// Background script to copy date-only from OOB due_date to u_new_due_date on sc_task
var gr = new GlideRecord('sc_task');
gr.addNotNullQuery('due_date');
gr.query();
while (gr.next()) {
var gdt = new GlideDateTime(gr.due_date);
var gd = gdt.getLocalDate();
gr.u_new_due_date = gd.toString();
gr.update();
count++;
gs.info('Updated sc_task ' + gr.number + ': ' + gr.due_date + ' -> ' + gr.u_new_due_date);
}
gs.ifno('Processed ' + count + ' tasks');
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
Since you want to extract only Date part from Due date field and set it in your custom field.
I tried to replicate the requirement in Incident form as shown below:
Before background script run:
After Background script run:
Here is script, you can tweak it according to your table/ requirement:
var gr = new GlideRecord('incident'); // insert your req table name
gr.addNotNullQuery('due_date'); // only records having due date
gr.query();
while (gr.next()) {
var gdt = new GlideDateTime(gr.due_date);
gr.u_date_due = gdt.getDate(); // Date field
gr.update();
}
If you find this helpful, please mark it as helpful,
Thanks and Regards,
Mohammed Zakir
