Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Age field on task table

snehalkhare
Kilo Guru

Hi All ,

I want to create a field which calculates age of the open tickets on the task table . I want it on Task table so that it can be used to calculate the age of all the tickets - incident , problem ,request etc.

How this can be done ? After creating this field it has to be used for reporting on task table , means all the open tickets -incident , problem ,request etc. ( ages ) can be represented in the report.

Thanks & Regards,

Snehal Khare

11 REPLIES 11

Aakash Shah4
Tera Guru

Try this



Create a new field of type duration on task table. You may name it as 'Age'.



Write an After-Update Business Rule with condition 'When State Changes To Resolved'



Script:



calculateAge();


function calculateAge(){


var datedif = gs.dateDiff(current.sys_created_on,current.resolved_at);


current.u_age = datedif;


current.update();


}


Hi Aakash,



I don't want the age of the resolved tickets , I want to calculate the age of the ongoing tickets on which the assignee's are still working on .


How will this calculate the ages for incidents ,problem etc.. I guess the state values will be different for different table


Thanks


Snehal Khare


Hi Snehal,



1. Create one Filed in Task table.


2. Create Schedule job Daily it will run . This scheduled job will calculate the Active tickets age how many days back ticket is created.



Scheduled job:



var gr = new GlideRecord("task");


gr.addEncodedQuery('sys_class_name=incident^ORsys_class_name=problem^ORsys_class_name=sc_req_item');


gr.addActiveQuery();


gr.query();


while (gr.next())


      {


      var currentTime = gs.nowDateTime(); // Get the current date time in users timezone


      var createdOn = gr.sys_created_on.getDisplayValue(); // Task created time in GMT(system default time zone)


      var datedif = gs.dateDiff(createdOn, currentTime,true); // calculate in seconds


      if(datedif<86400)


              {


              gr.u_age = 0;


      }


      else


              {


              gr.u_age = (datedif/86400);


      }


      gr.autoSysFields(false);


      gr.update();


}



Please Hit Correct if this helpful


Ujjawal Vishnoi
Mega Sage

Hi Snehal,



Perform the below mentioned steps.



1. Create a string type field on task table named 'Age'.


2. Create a schedule job as mentioned below. (Set the frequency of the job as per the frequency of the report run.)



var tsk= new GlideRecord('task');


tsk.addActiveQuery();


tsk.query();


while(tsk.next())


{


tsk.autoSysFields(false);


tsk.setWorkflow(false);


var inc_age =gs.dateDiff(tsk.sys_created_on.getDisplayValue(),gs.nowDateTime(),false);


var dur = new GlideDuration(inc_age);


dur=dur.getRoundedDayPart();     //This will give you age in days only


tsk.u_age=dur;       //u_age is the backend name of the age field


tsk.update();


}



Hope this helps.



Regards


Ujjawal