I am trying to update the duration field for the old tickets(incident and service requests) which are in resolved state. I tried with background script and scheduled jobs but no positive results. Any leads will be appreciated

No NaME
Mega Guru

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();

  }

}

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

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();


  }


}


View solution in original post

8 REPLIES 8

anurag92
Kilo Sage

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


Hi Anurag,



I tried changing it to OR but it is still not updating the duration field using the background script. and scheduler.



Thanks!


sharank1
Giga Contributor

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


Abhinay Erra
Giga Sage

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();


  }


}