Genpact Cora Knowledge Center

Support

Close a Task

Use the API to close a task according to the TaskInstanceId and ActivityInstanceId.

using System;
using System.Linq;
using PNMsoft.Sequence;
using PNMsoft.Sequence.Activities;
using PNMsoft.Sequence.Forms.Activities;
using PNMsoft.Sequence.Messaging;
using PNMsoft.Sequence.Runtime;
 
namespace SequenceEx.Tasks.Samples
{
    public class CloseTaskSample
    {
        public void CloseTaskByTaskInstanceId(int taskInstanceId)
        {
            TaskExecutor executor = TaskExecutor.GetExecutor(taskInstanceId);
 
            bool forceCloseActivity = true;
            executor.CompleteTask(forceCloseActivity);
        }
 
        public void CloseFirstTaskByActivityInstanceId(int activityInstanceId)
        {
            //If this code is executed in ESC you already have the execution context available
            IWorkflowExecutionService executionService =WorkflowRuntime.Engine.GetServiceWithCheck<IWorkflowExecutionService>();
            ActivityInstance activityInstance = executionService.GetActivityInstance(activityInstanceId);
 
            if (activityInstance == null)
            {
                throw new InvalidOperationException(string.Format(
                    "Cannot find the activity instance '{0}' because it does not exist or you do not have permissions.",
                    activityInstanceId));
            }
 
            if (activityInstance.Activity is IMessageActivity)
            {
                TaskInstance taskInstance = activityInstance.MessagesInstances
                    .OfType<TaskInstance>()
                    .FirstOrDefault();
 
                if (taskInstance != null)
                {
                    TaskExecutor executor = new TaskExecutor(taskInstance);
 
                    bool forceCloseActivity = true;
                    executor.CompleteTask(forceCloseActivity);
                }
            }
        }
}