Script for adding comments to a task

This article describes a method of adding comments to tasks automatically using scripts.
Let's take a look at two cases:

  • Adding a comment to any task
  • Adding a comment to a task of the current business process

Adding a comment to any task

Assume there is a task. In this example, a task is searched by a certain parameter (e.g. by ID).

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.
var task = PublicAPI.Portal.TaskBase.Task.LoadOrNull(380); // Load task by ID
PublicAPI.Portal.TaskBase.Task.AddComment(task, "Comment"); //Add a comment to the task

Script without PublicAPI

For the script to work correctly, add namespaces:

using EleWise.ELMA.Tasks.Managers;
Script text:
var instance = TaskBaseManager.Instance;
var task = instance.LoadOrNull(380); //Load a task by ID
instance.AddComment(task, "Comment"); //Add a comment to the task


Adding a comment to a task of the current business process

Let's take as an example a business process (see figure). There is a gateway in the process: on the first path, a user task is executed (Task 2), the second path ends with a script. If the second path is completed before the Task 2, then the script adds a comment to the Task 2. The graphic model of the process:

 

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.

Namespace:

using EleWise.ELMA.Tasks.Models;

Script text:

var nz = PublicAPI.Portal.TaskBase.Filter().Subject(context.X)  // find a task by name (subject)
                                    .Statuses(new List<TaskBaseStatus>
                                    {
                                        PublicAPI.Enums.Tasks.TaskBaseStatus.NewOrder,
                                        PublicAPI.Enums.Tasks.TaskBaseStatus.Read,
                                        PublicAPI.Enums.Tasks.TaskBaseStatus.InProgress
                                    }) //check the task status
                                    .Find();
foreach (var task in nz)
{
    PublicAPI.Portal.TaskBase.AddComment(task, context.Txt);
}

Script without PublicAPI

For the script to work correctly, add namespaces:

using EleWise.ELMA.Tasks.Models;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Tasks.Managers;

Script text:

var instance = TaskBaseManager.Instance;
var tsk = InterfaceActivator.Create<TaskBaseFilter>(); //create a filter on tasks
tsk.Subject = "Task 1"; // find a task by name (subject)
tsk.Statuses = new List<TaskBaseStatus>
    {
        TaskBaseStatus.NewOrder, 
        TaskBaseStatus.Read, 
        TaskBaseStatus.InProgress
    }; //check the task status
foreach (var task in instance.Find(tsk, null))
{
    instance.AddComment(task, "Comment"); //Add a comment to the task
}

The subject parameter is written to the X context variable. Afte the execution of the script, a comment will be added to the specified task (Task 2). If more than one task with this name is found in the process, then a comment will be added to all of them. The comment displays the information, specified in the context variable (in this case, Txt).