Collecting data of personal KPIs and writing the result to a total KPI

Assume there is a personal daily KPI - Number of Calls. This KPI measures the number of calls made by each operator every day. You need statistics, how many calls have been made by all the operators. The total number of calls (the sum of all the personal KPIs) will be written to a separate KPI - Total Number of Calls - which is not a personal KPI. 
To automatically calculate the total number of calls, create a script for collecting personal data. On the KPIs tab in ELMA Designer, go to the Collect Data section and add a new script. The script text is entered in the Execute method. 
After that, create a scheduler task, which will run the script for summing up the data, at the time specified on the Common tab. On the Tasks tab of this scheduler task, select the script. 
Publish the KPI model.

1. Script without using PublicAPI.

For the script to work correctly, add the namespaces:

using EleWise.ELMA.Services;
using EleWise.ELMA.KPI.Extensions;
using EleWise.ELMA.KPI.Security;
using EleWise.ELMA.Model.Common;
 

Script text:

//load the KPI, that stores the total number, to a local variable by name
var sum_indicator = IndicatorsService.Instance.LoadByName("Total number of calls");          
// Number of calls - personal KPI for each operator
//load it to the local variable by name as well
var sub_indicator = IndicatorsService.Instance.LoadByName("Number of calls");
// Create the dates that will indicate the KPI period
Pair<DateTime, DateTime> period = null;            
// Service for working with periodicities
var periodicityService = Locator.GetService<EleWise.ELMA.KPI.Common.Interfaces.IPeriodicityService>();
if (periodicityService != null)
{
    // Identify the period for writing the value
    period = periodicityService.GetPeriod(sub_indicator.Periodicity, dateToRun);
}            
if (period != null)
{
    // Create a context for calculating KPI values
    var calc_context = IndicatorsService.Instance.CreateContext(period, false);             
    if (calc_context != null)
        {
            // Get the users responsible for the KPI
            // For this, get the responsibility matrix
            var responsibleMatrix = sub_indicator.GetResponsiblesMatrix();                   
            // Get the set of permissions for the role of the responsible user Roles.Responsible
            var responsibleItem = responsibleMatrix.GetPermissionsByRole(Roles.Responsible).FirstOrDefault();                    
            // Get the users, responsible for the KPI
            var users = responsibleItem.GetAllUsers();                  
            double user_sum_value = 0;                  
            // Go through all the users
            foreach(var user in users)
            {
                // Get the KPI value over the period for a specific user
                var user_value = calc_context.GetValue(sub_indicator, period, user);
                //If the value for the current user exists,
                //increment the sum
                if (user_value.HasValue)
                    user_sum_value += user_value.Value;                     
            }
            //after calculating the sum, right it to the fact value of the total KPI
            //to fill in the KPI plan value, use the method SetPeriodPlanValue()
            IndicatorsService.Instance.SetPeriodValue(sum_indicator, period, user_sum_value);
        }
}
 

2. Script with PublicAPI.

Namespace:

using EleWise.ELMA.API;
 

Script text:

//load the KPI, that stores the total number, to a local variable by name
var sum_indicator = PublicAPI.KPI.Indicator.FindByName("Total number of calls");         
// Number of calls - personal KPI for each operator
//load it to the local variable by name as well
var sub_indicator = PublicAPI.KPI.Indicator.FindByName("Number of calls");
// Get the users responsible for the KPI
var users = PublicAPI.KPI.Indicator.GetPersonals(sub_indicator);
     
double user_sum_value = 0;
     
// Go through all the users
foreach(var user in users)
{
    // Get the KPI value over the period for a specific user
    var user_value = PublicAPI.KPI.Indicator.GetFactValue(sub_indicator,DateTime.Now,user);
    //If the value for the current user exists,
    //increment the sum
    if (user_value.HasValue)
                user_sum_value += user_value.Value;    
}
//after calculating the sum, right it to the fact value of the total KPI
//to fill in the KPI plan value, use the method PublicAPI.KPI.Indicator.SetPlanValue()
PublicAPI.KPI.Indicator.SetFactValue(sum_indicator,user_sum_value,DateTime.Now,PublicAPI.Portal.Security.User.GetCurrentUser()); //set the fact value