Script for fetching only date from OOB field ''Due Date' and updating it into new field Due Date

ShubhankarK9612
Giga Contributor

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.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@ShubhankarK9612 

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! 🙏

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

View solution in original post

5 REPLIES 5

Mohammed8
Giga Sage

Hi @ShubhankarK9612 

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:

 

Mohammed8_2-1767871270017.png

 

After Background script run:

 

Mohammed8_1-1767871228563.png

 

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