logo

NJP

Scheduled Job to calculate Business Duration and Duration field on TASK

Import · May 11, 2019 · article

Scheduled Job to calculate Business Duration and Duration field on TASK. It can be used for other task types just by change encodedquery.

var gr = new GlideRecord('task');
gr.addEncodedQuery('active=false^calendar_durationISEMPTY');
gr.autoSysFields(false); // so that the records don't have system updates
gr.query();
while(gr.next()) {
    var gdt1 = new GlideDateTime(gr.sys_created_on.getDisplayValue());
    var gdt2 = new GlideDateTime(gr.closed_at.getDisplayValue());
    var dur = gs.dateDiff(gdt1, gdt2, false);
    gr.calendar_duration = dur;
    var gsBusiness =new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //sysid of schedule
    // Get duration based on schedule
    gr.business_duration = gsBusiness.duration(gr.sys_created_on.getGlideObject(), gr.closed_at.getGlideObject());
    gr.setWorkflow(false);
    gr.update();
}

image

View original source

https://www.servicenow.com/community/developer-articles/scheduled-job-to-calculate-business-duration-and-duration-field/ta-p/2322628