Collecting data on a personal KPI
This article describes how to get the list of users, responsible for a KPI and set a value over a period to each of them.
Script without PublicAPI.
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 by ID
var indicator = IndicatorsService.Instance.LoadByName("Number of calls daily");
// 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)
{
// Define the period for writing a value
period = periodicityService.GetPeriod(indicator.Periodicity, dateToRun);
}
if (period != null)
{
// Get the users responsible for the KPI
// For this, get the responsibility matrix
var responsibleMatrix = indicator.GetResponsiblesMatrix();
// Get the set of permissions for the responsible user's role Roles.Responsible
var responsibleItem = responsibleMatrix.GetPermissionsByRole(Roles.Responsible).FirstOrDefault();
// Get the users, responsible for the KPI
var users = responsibleItem.GetAllUsers();
// Go through all the users
foreach(var user in users)
{
// Assign a constant value
// Here you can use a request, for example, to an external service
var value = 123.45;
// Save the value to the database
IndicatorsService.Instance.SetPeriodValue(indicator, period, value, "Script", user);
}
}
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
Namespace:
using EleWise.ELMA.API;
Script text:
// Load the KPI by name
var indicator = PublicAPI.KPI.Indicator.FindByName("Number of calls daily");
// Get the users, responsible for the KPI
var users = PublicAPI.KPI.Indicator.GetPersonals(indicator);
// Go through all the users
foreach(var user in users)
{
// Assign a constant value
// Here you can use a request, for example, to an external service
var value = 123.45;
// Save the value to the database
PublicAPI.KPI.Indicator.SetFactValue(indicator,value,DateTime.Now,user, "Script"); //sets the fact value
//PublicAPI.KPI.Indicator.SetPlanValue(indicator,value,DateTime.Now,user, "Script"); //sets the plan value
}