display number of open Change Tasks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 01:28 PM
Hi,
I would like to display a number on the change form which shows the number of open change tasks for that change.
The field will be a String called Open Tasks.
Does anyone know how to do this or has a script which would display this on the Change form?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 01:57 PM
Write a display business rule on the change record
In the script:
var grCT = new GlideRecord('change_task');
grCT.addQuery('change_request',current.sys_id);
grCT.addQuery(<your query to check open tasks>);
gr.query();
current.<open task field> = gr.getRowCount();
This will refresh your count everytime someone opens the change record.
Alternatively, you can reduce the count when the task is closed as well. In that case, you will need to write an after update business rule on the change task table, and when the task state changes to closed, reduce the count of related change request->open tasks by 1. You will also need to make sure to increase the count as well when a new task is added.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 02:02 PM
Hi Riaz,
Try putting a task count field on your change_request record. Then use an after update BR like this to keep the value current:
(function executeRule(current, previous /*null when async*/) {
var theAggs = new GlideAggregate("incident_task");
theAggs.addQuery("incident", current.incident);
theAggs.addQuery("u_integer_value", "!=","");
theAggs.addAggregate("SUM", "u_integer_value");
theAggs.groupBy("incident");
theAggs.query();
var theTotal = 0;
while (theAggs.next()) {
theTotal += parseInt(theAggs.getAggregate("SUM", "u_integer_value"), 10);
}
var theParent = current.incident.getRefRecord();
theParent.setValue("u_task_count", theTotal);
theParent.update();
})(current, previous);
With that in place, you just add the field to your form.
Hope that helps.
:{)
Helpful and Correct tags are appreciated and help others to find information faster
:{)
Helpful and Correct tags are appreciated and help others to find information faster

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 02:03 PM
Hi Riaz,
Yes you can achieve this via before-insert business rule and it will populate the field with the total count of the change task. But just wondering why it is required? OOB There is a related list available which shows the change task with the numbers, state and other details with the link then why you need the dedicated field on the form? Just curious to know!
Kindly mark my answer as Correct and Helpful based on the Impact.
Regards,
Alok

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 02:10 PM
If you need the field for special business requirement then you can create a before-update business rule and you the below script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord('change_task');
gr.addQuery('change_request',current.getUniqueValue());
gr.addActiveQuery();
gr.query();
current.field_name=gr.getRowCount();//replace field_name with the field name where you want the count to be populated.
})(current, previous);
Kindly mark my answer as correct and helpful based on the impact.
Regards,
Alok