How to enter related incident count of a problem into a field in the problem form?

peterwoodward
Giga Contributor

On the Problem form I have added a field called Related Incident Count.  I need to populate this field with the number incidents that are related to the problem for all open problems.  This way the field can be a column included in a report that can either be printed or appear in a widget on a homepage.

I found a business rule that will display the number of related incidents in the Related Incident Count field.  But because it is a display business rule, it only appears there when the problem is opened.  Because of this, the field does not actually contain any data, so nothing appears in the column of the report.

How do I change this display BR to actually enter the number into the field?

Here is the script:

On the Problem table

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var gr = new GlideRecord("incident");
gr.addQuery('problem_id', "=", current.sys_id);
gr.query();
if (gr.next()) {
}
//custom string field to store count
current.u_related_incident_count= gr.getRowCount();

})(current, previous);

I have attached a screenshot of the BR.  Any assistance with this is greatly appreciated.

1 ACCEPTED SOLUTION

DScroggins
Kilo Sage

Hello,

 

Few things it is not best practice to perform record updating in display business rules. Also when you simply need the count of records using GlideAggregate is best practice and provides better performance as you do not need to return all the records just to get a count. This is what getRecordCount() does. So one thing you could do is add an After Business Rule to the incident table when Problem field changes. The BR would look like:

find_real_file.png

 

The advanced script would count all incidents related to the same problem then update the problem record field. If the problem field changes to blank the script will use the previous value:

(function executeRule(current, previous) {
    var incCount = 0;
    var prbSysID = (!current.problem_id.nil())? current.problem_id : previous.problem_id;
	
    var ga = new GlideAggregate("incident");
    ga.addAggregate('COUNT');
    ga.addQuery('problem_id', prbSysID);  
    ga.query();
    if (ga.next()) {
        incCount = ga.getAggregate('COUNT');
    }

    var problemGR = new GlideRecord('problem');
	problemGR.get(prbSysID);
        problemGR.setValue('u_related_incident_count', incCount);
	problemGR.update();
    

})(current, previous);

 

Hope this helps.

--David

 

 

View solution in original post

7 REPLIES 7

Community Alums
Not applicable

Hi,

 

Why are you not making use of the OOTB field on problem table for this?

 

find_real_file.png

Our instance does not have a field like what you have shown.  This is why I am attempting to do it now.

DScroggins
Kilo Sage

Hello,

 

Few things it is not best practice to perform record updating in display business rules. Also when you simply need the count of records using GlideAggregate is best practice and provides better performance as you do not need to return all the records just to get a count. This is what getRecordCount() does. So one thing you could do is add an After Business Rule to the incident table when Problem field changes. The BR would look like:

find_real_file.png

 

The advanced script would count all incidents related to the same problem then update the problem record field. If the problem field changes to blank the script will use the previous value:

(function executeRule(current, previous) {
    var incCount = 0;
    var prbSysID = (!current.problem_id.nil())? current.problem_id : previous.problem_id;
	
    var ga = new GlideAggregate("incident");
    ga.addAggregate('COUNT');
    ga.addQuery('problem_id', prbSysID);  
    ga.query();
    if (ga.next()) {
        incCount = ga.getAggregate('COUNT');
    }

    var problemGR = new GlideRecord('problem');
	problemGR.get(prbSysID);
        problemGR.setValue('u_related_incident_count', incCount);
	problemGR.update();
    

})(current, previous);

 

Hope this helps.

--David

 

 

Thank you for your prompt reply.  I will give it a go and let you know how it turns out.