How to get total count of Incidents linked to a Problem record (not just incrementing)?

schetry
Tera Contributor

 

Hi everyone,

 

I'm working on a use case where I need to display the total number of Incidents linked to a Problem record in a custom integer field (u_incident_count).

Here's the Business Rule I wrote (on the Incident table, after insert/update, runs when problem_id changes):

 

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('problem');
gr.addQuery('sys_id', current.problem_id);
gr.query();
if (gr.next()) {
gr.u_incident_count = gr.u_incident_count + 1;
gr.update();
}
})(current, previous);
 

It doesn't calculate the actual total number of incidents currently associated with the problem.

 

What I need instead is:

Every time an incident is linked or unlinked, the problem’s u_incident_count should reflect the total number of incidents currently linked.

Is there a better approach to achieve this?  Any suggestions or best practices are appreciated!

 

Thanks in advance,
Sidhz

1 ACCEPTED SOLUTION

GlideFather
Tera Patron

Olá @schetry,

First of all, GlideRecord is great when you need to retrieve details or update the record, but if you are just good with a number of records, then you should go via GlideAggregate, it is better in many aspects.

For example here:
https://www.servicenow.com/community/developer-articles/difference-between-gliderecord-and-glideaggr...

 

Secondly, why did you write it in a BR? If you want to know the exact number of records, you could do it via scheduled job (triggered: daily, weekly, monthly, on demand) to get that number.

 

Business rule has trigger conditions (e.g. incident is closed, or user is deactivated, ...), thus this is not the best idea for counting records...

 

Or it can be a part of report/dashboard...

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


View solution in original post

1 REPLY 1

GlideFather
Tera Patron

Olá @schetry,

First of all, GlideRecord is great when you need to retrieve details or update the record, but if you are just good with a number of records, then you should go via GlideAggregate, it is better in many aspects.

For example here:
https://www.servicenow.com/community/developer-articles/difference-between-gliderecord-and-glideaggr...

 

Secondly, why did you write it in a BR? If you want to know the exact number of records, you could do it via scheduled job (triggered: daily, weekly, monthly, on demand) to get that number.

 

Business rule has trigger conditions (e.g. incident is closed, or user is deactivated, ...), thus this is not the best idea for counting records...

 

Or it can be a part of report/dashboard...

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */