Interrupting approval tasks using a script

The script interrupts active approval tasks of a specific document.

Script with PublicAPI

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

Context variable:

context.Document - variable of the Document type with the List link type

Namespace:

using EleWise.ELMA.API;

Script text:

foreach (var group in PublicAPI.Docflow.Tasks.GetActiveApprovementGroups(context.Document))
{
    PublicAPI.Docflow.Objects.Tasks.ApprovementTaskGroup.Terminate(group, false);
}

Script without PublicAPI

For the script to work, add the assemblies:

EleWise.ELMA.Documents
EleWise.ELMA.Documents.Docflow

Namespaces:

using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Documents.Models.Tasks;
using EleWise.ELMA.Tasks.Models;
 
Script text:
//Here active approval sheets are loaded (the document must be stored in the context.RentContract variable)
var groups = ApprovementTaskGroupManager.Instance.GetActive(new List<IDocument> { context.RentContract }).ToList();
//Go through the active approval sheets
groups.ForEach(g =>
{
    //Set the Interrupted status for the sheets
    g.ApprovementList.Items.ToList().ForEach(i =>
    {
        i.Results.ToList().ForEach(r =>
        {
            if (r.Status == ApprovalStatus.None)
            {
                r.Status = ApprovalStatus.Terminated;
                r.Save();
            }
        });
    });
    //Set the Interrupted status for the approval task
    g.Tasks.ToList().ForEach(t =>
    {
        if (t.ApprovalStatus == ApprovalStatus.None)
        {
            if (t.IsActive())
                t.Status = TaskStatus.WasClosed;
            t.ApprovalStatus = ApprovalStatus.Terminated;
            t.ListItem.Results.ToList().ForEach(r =>
            {
                if (r.Status == ApprovalStatus.None)
                {
                    r.Status = ApprovalStatus.Terminated;
                    r.Save();
                }
            });
            t.Save();
        }
    });
    //Set the status for the approval sheet
    g.Status = ApprovalStatus.Terminated;
    g.Save();
});