How to get the count of elements that is selected in breakdown on dashboard

Krithika R
Tera Contributor

Is there any method or function where we can get the count of how many breakdown elements which are selected in the dashboard or analytical hub.

I know there is a method pa.getCurrentElementID() which is return the sys_id of the element which has been selected in dashboard or analytical hub, similarly, is there any method where we can get the count like 2 if 2 element have been selected in dashboard or analytical hub. So that I can divide that with the count indicator.

If there is no such method is there any way, it can be achieved

Any ideas or help is appreciated.

1 REPLY 1

SwarnadeepNandy
Mega Sage

Hello @Krithika R,

 

As far as I know, there is no built-in method or function that can directly return the count of selected breakdown elements in ServiceNow dashboard or analytical hub. However, there are some possible ways to achieve this functionality using custom scripts or widgets.

One way is to use the pa.getCurrentElementIDs() method, which returns an array of sys_ids of the selected breakdown elements. You can then use the array.length property to get the count of the array elements. For example, you can use something like this in a script include:

var elementIDs = pa.getCurrentElementIDs(); 
var elementCount = elementIDs.length;

Another way is to use the pa.getBreakdownElements() method, which returns an array of objects containing information about the breakdown elements. You can then use the array.length property to get the count of the array elements. For example, you can use something like this in a widget script:

var elementObjects = pa.getBreakdownElements(); 
var elementCount = elementObjects.length;

You can also use the GlideAggregate class to perform a count query on the breakdown source table and filter by the selected breakdown elements. For example, you can use something like this in a script include:

var ga = new GlideAggregate(‘breakdown_source_table’); 
ga.addAggregate(‘COUNT’); 
ga.addQuery(‘sys_id’, ‘IN’, pa.getCurrentElementIDs().join(‘,’)); ga.query(); 
if (ga.next()) {
 var elementCount = ga.getAggregate(‘COUNT’); 
} 

 

Not sure, if it would help or not, but you can give it a try.

 

Kind Regards,

Swarnadeep Nandy