Creating and using custom activities

This article provides an example of creating a custom activity for calculating date/time values in a business process.

Custom activity – is an activity placed on the process graphic model, which executes certain actions, defined by the scripts of the custom activity. Custom activities allow using the same script in different processes. To use ready-made activities you do not need any skills in writing scripts.

Calculating date/time is often required in business processes, designed to ensure compliance with the execution time of the operations. Date/time values can be calculated taking into account the business calendar, or not.

In this example, we are going to create a custom activity that will take a source date, the number of days, hours and minutes added to the source date and the calculation taking into account the business calendar parameter as input values. The custom activity will return the date and time calculated based on the input values.

To learn how to create a custom activity, read Help.

Let's define the context variables on the Parameters tab of the custom activity:

  • Source date – Date/Time type, Input
  • Days added – Integer type, Input
  • Hours added – Integer type, Input
  • Minutes added – Integer type, Input
  • Take business calendar into account – Yes/No type, Input
  • Calculated date – Date/Time type, Output

To calculate the date taking the business calendar into account, add namespaces:

using EleWise.ELMA.Scheduling;
using EleWise.ELMA.Services;

In the script, first, check that the input parameters are filled in. If the value of the Source date parameter is not specified, the current date and time are used; for the parameters Days added, Hours added, Minutes added the default value is 0. For the parameter Take business calendar into account the default value is No.

Based on the specified or default parameter values, identify the time to add:

TimeSpan ts = new TimeSpan(parameters.DaysAdded, parameters.HoursAdded, parameters.MinutesAdded, 0);

To calculate the date taking the business calendar into account, use the following script:

var calendar = Locator.GetServiceNotNull();
parameters.CalculatedDate = calendar.EvalTargetTime(parameters.SourceDate.Value, ts);

If the business calendar is not taken into account, use this script:

parameters.CalculatedDate = parameters.SourceDate.Value.Add(ts);

The full script of the custom activity looks as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
using EleWise.ELMA.Services;
using EleWise.ELMA.Scheduling;
using Parameters = EleWise.ELMA.Workflow.Models.Parameters.CA_VychislenieDaty;
  
namespace EleWise.ELMA.Model.Scripts
{
    ///
    /// "Calculate Date" Custom Activity script module
    ///
    //Attention! For correct work, do not change the class name
    public class CA_VychislenieDaty_Scripts
    {
  
        //Attention! The class must contain the Execute method with this signature
        public void Execute(Parameters parameters)
        {
               //If the value of the Source Date parameter is not specified, the current date and time is used
               if (parameters.SourceDate == null) parameters.SourceDate = DateTime.Now;
               //For the parameters Days Added, Hours Added and Minutes Added
               //the default value is 0
               if (parameters.DaysAdded == null) parameters.DaysAdded = 0;
               if (parameters.HoursAdded == null) parameters.HoursAdded = 0;
               if (parameters.MinutesAdded == null) parameters.MinutesAdded = 0;
               //For the parameter Take business calendar into account the default value is No
               if (parameters.UseBusinessCalendar == null) parameters.UseBusinessCalendar = false;
  
               TimeSpan ts = new TimeSpan((int)parameters.DaysAdded, (int)parameters.HoursAdded, (int)parameters.MinutesAdded, 0);
               if (parameters.UseBusinessCalendar)
               {
                       var calendar = Locator.GetServiceNotNull<IProductionCalendarService>();
                       parameters.CalculatedDate = calendar.EvalTargetTime(parameters.SourceDate.Value, ts);
               }
               else
               {
                       parameters.CalculatedDate = parameters.SourceDate.Value.Add(ts);
               }
        }
    }
}

After creating a custom activity, you need to publish it. Publishing custom activities does not require restarting the server.

 

After publishing, the custom activity will be available in the Plug-ins unit on the left panel of a process page.

Consider a simple process of issuing an invoice. When modeling this process, an analyst does not need to write any scripts. It is sufficient to specify the values of the parameters, configured in the custom activity.

A sales rep issues an invoice, specifying its date, number, and the contractor. After that, the accountant confirms receiving a payment. The invoice is valid within three business days from the date of issuing. If the invoice is not paid on time, the sales rep issues the invoice again.

Settings of the Issue Invoice task.

Input/output attributes of the Calculate Date custom activity.

Timer escalation from the Confirm Payment activity.