- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2016 10:14 PM
calculatedura();
function calculatedura() {
var taskRec = new GlideRecord('task');
taskRec.addQuery('state', '=', 6);
taskRec.addQuery('state', '=', 7);
var orCondition = taskRec.addQuery('sys_class_name', 'incident');
orCondition.addOrCondition('sys_class_name', 'u_request');
taskRec.query();
while (taskRec.next()) {
var ageInt = gs.dateDiff(taskRec.u_created_at.getDisplayValue(), taskRec.u_resolved.getDisplayValue(), true);
taskRec.calendar_duration = ageInt;
taskRec.update();
}
}
Solved! Go to Solution.
- Labels:
-
Dashboard
-
Performance Analytics
-
Reporting

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2016 01:39 PM
Here you go. You need to pass the third parameter as false in gs.dateDiff() function.
calculatedura();
function calculatedura() {
var taskRec = new GlideRecord('task');
taskRec.addQuery('state', '=', 6);
taskRec.addQuery('state', '=', 7);
var orCondition = taskRec.addQuery('sys_class_name', 'incident');
orCondition.addOrCondition('sys_class_name', 'u_request');
taskRec.query();
while (taskRec.next()) {
var ageInt = gs.dateDiff(taskRec.u_created_at.getDisplayValue(), taskRec.u_resolved.getDisplayValue(), false);
taskRec.calendar_duration = ageInt;
taskRec.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2016 02:36 AM
taskRec.addQuery('state', '=', 6); taskRec.addQuery('state', '=', 7); These 2 lines are basically an 'AND' between conditions, and this will always evaluate to 0 results. Try putting 'OR' just like you have done between request and inc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2016 01:24 PM
Hi Anurag,
I tried changing it to OR but it is still not updating the duration field using the background script. and scheduler.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2016 01:35 PM
Munish,
I was also in need of same requirement. i achieved it with creating a new field 'aging' on task table. Then I have created a Scheduled job which calculates the difference of opened date and current date.
for the new field, you can define your choices as required.
Code:
updateAging();
function updateAging() {
var elapsedTime = 0;
var aging = '';
var currentTimeNow = gs.nowDateTime();
var gr = new GlideRecord('task');
gr.addEncodedQuery('active=true');//active tasks, incidents, changes
gr.query();
while(gr.next()) {
elapsedTime = (gs.dateDiff(gr.opened_at, currentTimeNow, true))/60/60/24;
gs.log(elapsedTime);
//check to see when the item was created
if (elapsedTime <= 5) aging = '0_5'; // Values of choices are mentioned here i.e.'0_5'
if (elapsedTime > 5 ) aging = '6_15';
if (elapsedTime > 15) aging = '16_30';
if (elapsedTime > 30) aging = '31';
gs.log(aging);
gr.setWorkflow(false); //skip any Business Rules
gr.autoSysFields(false); //do not update system fields
gr.xxx = aging; // xxx should be back end name of the field you create on task table
gr.update();
}
}
Thanks,
Sharan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2016 01:39 PM
Here you go. You need to pass the third parameter as false in gs.dateDiff() function.
calculatedura();
function calculatedura() {
var taskRec = new GlideRecord('task');
taskRec.addQuery('state', '=', 6);
taskRec.addQuery('state', '=', 7);
var orCondition = taskRec.addQuery('sys_class_name', 'incident');
orCondition.addOrCondition('sys_class_name', 'u_request');
taskRec.query();
while (taskRec.next()) {
var ageInt = gs.dateDiff(taskRec.u_created_at.getDisplayValue(), taskRec.u_resolved.getDisplayValue(), false);
taskRec.calendar_duration = ageInt;
taskRec.update();
}
}