logo

Accessing the context of the parent process instance from a script

This article provides examples of scripts for getting/changing simple and block context variables of the parent process.

A script for passing a block from the parent process to the subprocess is also provided.
A subprocess script for changing the value of a context variable of the parent process.
Add global assembly:
Microsoft.CSharp
Namespace:
using Microsoft.CSharp;
 
Script text:
var parentInstance = context.WorkflowInstance.ParentInstance;
if (parentInstance != null)
            {
             // There is a parent instance
             // Get the parent instance context
             dynamic parentContext = parentInstance.Context;
             // Set the property value from the parent instance context
             parentContext.Value = context.Value;
            }
 
 
A script for creating block items in the parent process from the subprocess.
Add assemblies:
EleWise.ELMA.Workflow.Processes
ICSharpCode.NRefactory (global assembly)
Microsoft.CSarp (global assembly)
Namespaces:
using EleWise.ELMA.Model.Entities.ProcessContext;
using EleWise.ELMA.Model.Services;
using Microsoft.CSharp;
 
Also, describe the function used in the script before the script itself:
// Function that creates a block item:
     private dynamic CreateParentItem(object parentTablePart)
     {
      if (parentTablePart == null)
      {
       throw new InvalidOperationException("Block does not exist");
      }
      var itemType = parentTablePart.GetType().GetInterface(typeof(Iesi.Collections.Generic.ISet<>).FullName).GetGenericArguments()[0];
      return InterfaceActivator.Create(itemType);
     }
 
Script text:
var parentInstance = context.WorkflowInstance.ParentInstance;
         if (parentInstance != null)
         {
             // There is a parent instance
              // Get the context of the parent instance
             dynamic parentContext = parentInstance.Context;
             // Create an item in the parent block
             var item = CreateParentItem(parentContext.Block);
             // Assign the variable values in this block item
             item.BlockRow = context.String;
             // Add an item to the block
             parentContext.Block.Add(item);
         }
 
 
A script for passing a block from the parent process to a subprocess.
Add assemblies:

EleWise.ELMA.Workflow.Processes
ICSharpCode.NRefactory (global assembly)
Microsoft.CSarp (global assembly)

Namespaces:
using EleWise.ELMA.Extensions;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Entities.ProcessContext;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Types.Settings;
using Microsoft.CSharp;
 
Script text:
var parentInstance = context.WorkflowInstance.ParentInstance;
if (parentInstance != null)
{
dynamic parentContext = parentInstance.Context;
foreach (var t in (Iesi.Collections.ISet)parentContext.Block)
{  var b = new P_Pod_test_PodBlock();//create an item in the subprocess block
b.String=t.AsDynamic().BlockRow;//get the value of the parent block
context.PodBlock.Add(b);
}
}

 

Note
When working with the "parentContext" variable, autocomplete in the script editor will not work. Moreover, when publishing, the correctness of property names, refered to in the parentContext, is not checked. I.e. if you write "parentContext.Value123", while there is no Value123 variable in the parent process, the process will be published, but an error will occur during the script execution. Therefore you should pay attention when working with the context of the parent instance. To be safe, you can wrap the code in "try { } catch {}" to process the error during the execution.