Display BR

Apaul
Tera Contributor

create a display business rule to show number of related tasks in a table.

7 REPLIES 7

sunil maddheshi
Tera Guru

@Apaul 

Lets say you want to count no of related incidents on task table, below is display BR:

var taskCount = 0;
    
    var grTask = new GlideRecord('task'); // Fetch tasks related to the incident
    grTask.addQuery('parent', current.sys_id); // Parent field links tasks to incident
    grTask.query();
    
    if (grTask.hasNext()) {
        taskCount = grTask.getRowCount();
    }
    gs.addInfoMessage(taskCount);

Please mark correct/helpful if this helps you!

Hello @Apaul ,

I hope you are good.

If someone ever asks you about write or tell how to fetch the count it is always best practice to use GlideAggregate.


var taskCount = 0;

var aggTask = new GlideAggregate('task'); // Use GlideAggregate to count tasks efficiently
aggTask.addQuery('parent', current.sys_id); // Filter tasks related to the current incident
aggTask.addAggregate('COUNT'); // Aggregate function to count records
aggTask.query();

if (aggTask.next()) {
taskCount = aggTask.getAggregate('COUNT'); // Get the count value
}

gs.addInfoMessage(taskCount);

the above script will give you the count of tasks.

Display Business rule: This type of business rule can be used in  different scenarios 
One example Whenever a kb article is loaded /viewd you want to increase the view count in this case you can use a display business rule to achieve this.
(function executeRule(current, gsr, gs) {
// Increment the view count for the KB article
current.view_count = (current.view_count || 0) + 1;
current.update(); // Save the updated view count
})(current, gsr, gs);


Runs before the KB article is sent to the user.
Increments the view_count field by 1 every time the article is loaded.
Uses current.update(); to save the new count back to the database.


Business rule - https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/script/business-rules/concept/c_B...

Please Mark it as correct/helpful if this helps you