Incident task count query

venkatkk
Tera Contributor

We have Incident and Incident task associated for the Incident. We like to know how many (count) incidents and status of the task task assigned to parent Incident into a separate field called "Task Status & Count" in Incident form.

Incident Table                 :-       Incident

Incident task Table   :-   u_incident_task

New field :-                                       u_task_count_status

I tried with the below query BR on display but it is not working

Table : Incident

var inctask = new GlideRecord('u_incident_task');

  inctask.addQuery('parent', current.sys_id);

  inctask.addActiveQuery();

  inctask.query();

  if (inctask.next()) {

  gs.addInfoMessage('Tasks :   ' + inctask.getRowCount());

  current.u_task_count_status = (sctask.getRowCount() + '-' + inctask.status);

  }

  }

Please help

1 ACCEPTED SOLUTION

If thats your requirement, then you have to write an After BR on Incident task.


You are updating a field in Incident table on the basis of the incident task.


So when any new incident task gets created - query the Parent field of Incident task and get the corresponding Incident sys_id


And make the update on the Incident table's field task count status field



BR on incident task table


Insert and Update


Condition = current.active.changes()


Script -


var grInc = new GlideRecord('incident');


if(grInc.get(current.parent)){


    if(current.active){


        //default task count status to zero


        //if any new incident task is created, then count increase


          grInc.u_task_count_status = grInc.u_task_count_status + 1;


  }


    else{


          //if the incident task is closed, then count decrease active tasks


          grInc.u_task_count_status = grInc.u_task_count_status - 1;


    }


}



Hopefully this helps


View solution in original post

17 REPLIES 17

If thats your requirement, then you have to write an After BR on Incident task.


You are updating a field in Incident table on the basis of the incident task.


So when any new incident task gets created - query the Parent field of Incident task and get the corresponding Incident sys_id


And make the update on the Incident table's field task count status field



BR on incident task table


Insert and Update


Condition = current.active.changes()


Script -


var grInc = new GlideRecord('incident');


if(grInc.get(current.parent)){


    if(current.active){


        //default task count status to zero


        //if any new incident task is created, then count increase


          grInc.u_task_count_status = grInc.u_task_count_status + 1;


  }


    else{


          //if the incident task is closed, then count decrease active tasks


          grInc.u_task_count_status = grInc.u_task_count_status - 1;


    }


}



Hopefully this helps


Thanks Srikanth,


Will this Update for exiting records?


This will help you sort the future records, for existing Incident records, you may have to write a Fix Script


-


FYI, Fix Scripts - ServiceNow Wiki


Script:


var grInc = new GlideRecord('incident');


grInc.addActiveQuery(); //updating only for active incidents


grInc.query();



while(grInc.next()){


        var grIncTask = new GlideRecord('incident_task');


        grIncTask.addActiveQuery();


        grincTask.addQuery('parent',grInc.sys_id);


        grIncTask.query();


     


        grInc.setValue('u_task_count_status',grIncTask.getRowCount());


        grInc.setWorkflow(false); //you dont want to run any business rule with this fix script


        grInc.update();


}




Hopefully this helps


I tried with the above script the count is not populating, it is empty, thing code is not working.


function onAfter(current, previous) {


  //This function will be automatically called when this rule is processed.


  var grInc = new GlideRecord('incident');


  if(grInc.get(current.parent)){


  gs.log("Parent=====>"+ grInc.get(current.parent));


  if(current.active){


  gs.log("Coming Inside1");


  grInc.u_ task_count= grInc.u_ task_count + 1;


  }


  else{


  gs.log("Coming Inside2");


  grInc.u_ task_count = grInc.u_ task_count - 1;


  }


  }


}




Log says "Coming inside 1" but the count is not populating it is empty in the field