Filling in process metrics using scripts

In ELMA you can create metrics/KPIs of a process or a process instance.

This article describes scripts for filling in metric values using process scripts.

For example, the time of processing a request in a business process is specified by a user in the context variable ProcessingTime. This time must be written to the process instance metric RequestProcessingTime.

To do so, add the following script to the business process:

// Get the process instance metric and cast it to the type P_ProcessWithMetrics_IM
var metricValues = context.WorkflowInstance.MetricValues as P_ProcessWithMetrics_IM; 
// Set the value of the ProcessingTime context variable to the process instance metric
metricValues.RequestProcessingTime = Convert.ToInt32(context.ProcessingTime); 
// Save the process instance metric

You can find the type to cast the metric to in the business process settings:

Another task that may arise is saving the number of process starts for a personal metric.

In this case, you work with a process metric instead of a process instance metric.

Create a process metric with the following parameters:

The script for filling in the process metric:

var processMetrics = context.WorkflowInstance.Process.ProcessMetrics.EntityProperties.Cast<ProcessMetric>();
if(processMetrics != null){
    //search a process metric by name
    var isp = processMetrics.FirstOrDefault(pm => pm.Name == "NumberOfStarts");
     if (isp != null){     
        //if the process metric is personal, specify the context variable that will store the user who started the process; otherwise, instead of context.Executor use null          
        SaveMetricValue(isp, context.Executor);

Also, add the metric value saving method to the process script:

private void SaveMetricValue(ProcessMetric metric, IUser responsible){
    var filter = InterfaceActivator.Create<IProcessMetricValueFilter>();     filter.DisableSecurity = true;
     filter.Responsible = responsible;
     filter.ProcessMetricUid = metric.Uid;
     if(metric.UsePeriodicity && metric.PeriodicityUid.HasValue){
         var periodService = Locator.GetServiceNotNull<IPeriodicityService>();
         var periodicity = periodService.Load(metric.PeriodicityUid.Value);
         if (periodicity != null){
             var period = periodService.GetPeriod(periodicity, DateTime.Now);
             filter.PeriodStart = period.First;
             filter.PeriodEnd = period.Second;
     IProcessMetricValue ispValue = ProcessMetricValueManager.Instance.Find(filter, FetchOptions.All).FirstOrDefault();
     if (ispValue != null){
         double doubleValue;
         if (double.TryParse(ispValue.Value.ToString(), out doubleValue)){
             ProcessMetricValueManager.Instance.MetricSave(metric, responsible, doubleValue + 1);
         } else {
             Logger.Log.Error(String.Format("Error calculating the {0} KPI", metric.DisplayName));
         ProcessMetricValueManager.Instance.MetricSave(metric, responsible, 1);


using EleWise.ELMA.Workflow.Models;
using EleWise.ELMA.Security.Models;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Services;
using EleWise.ELMA.KPI.Common.Interfaces;
using EleWise.ELMA.Workflow.Managers;
using EleWise.ELMA.Logging;