Script for distinct count in automated indiactor in peromance analytics

Tippireddy Venk
Tera Contributor

We have two tables: Division and Activity. The Division data is being used within the Activity table as a list-type field (List Collector).

To determine the number of divisions associated with each activity, we implemented the following approach:

  • Created a Performance Analytics automated indicator
  • Added a script to calculate the count of divisions selected in the list field

function count() {
    var divsion_id = current.sys_id;

    var activity = new GlideRecord("x_cenll_strategi_0_activity");
    activity.addEncodedQuery("divisionLIKE" + divsion_id);
    activity.query();
    if (activity.next()) {
        divsion_id;
    }
   
}
count();
 
but script is not working

TippireddyVenk_0-1779790440190.png

 

1 REPLY 1

Mark Manders
Giga Patron

Your 'if' will only return the first one and doesn't do anything after that. However, that may not even be the issue. You mention 'script is not working'. But you don't mention what isn't working. Doesn't it return anything? Too much? Too little? 

Depending on what isn't working, what you are getting returned and what you are expecting to return, something like this could help:

function execute() {
    var divisionSysId = current.getValue('sys_id');
    var gaActivity = new GlideAggregate('x_cenll_strategi_0_activity');
    gaActivity.addQuery('division', 'CONTAINS', divisionSysId);
    gaActivity.addAggregate('COUNT');
    gaActivity.query();
    if (gaActivity.next()) {
        return parseInt(gaActivity.getAggregate('COUNT'), 10);
    }
    return 0;
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark