Code Activity

The Code activity allows you to write custom logic; therefore, you can augment the functionality of a workflow to meet your needs.

The Code activity only supports the language VB.NET. Using VB.NET, you can enter any valid snippet of code to accomplish specific needs based on the code entered. Using the Code activity, you can create parameters in the code body that can be bound to workflow fields, parameters, external variables, activity properties, and any additional bindable resources. However, you cannot directly access resources in the application, or any external resource.
Important: When a code activity runs in a workflow, no other activity can run, even within a parallel activity. Therefore, avoid writing long-running code blocks.
To access external resources, running workflow resources must be passed in as parameters to the code. While a workflow can access only Workflow and main system assemblies, it can also import namespaces from external sources. All scripting within the Code activity is entered into the script editor.
Important: When writing your code, ensure that you call Dispose on all objects that implement IDisposable created by your code. For example, if you open a file, you must also ensure that you close that file.

The following functions are supported for the Code activity:

Example supported Code functionsUnsupported Code functions
Variable declarations and assignments (Dim i as Integer; i = 5)Declaring a class, interface, structure, or enum.
Math operations (i = 5*j -100)Declaring a subprocedure or function.
Flow control logic (If/Else statements, While loops, goto’s)Declaring a namespace.
Calling member methods of a declared object, or an object passed in as a parameter (MyObj.DoSomething())Importing a namespace.
Calling static methods on known/imported typesReturning a value: Code activities have no return values, they must provide output through code parameters (for example, you can call ‘return’ to indicate you want the snippet to end, but you cannot call ‘return true’).
Try-catch statementsDirectly accessing properties of the workflow (activities, variables, and so on). You must pass any values you want to work with as parameters.
Throwing exceptionsCalling another code snippet in another activity.

Data Types

All system and application data types are supported. Importing namespaces is performed by using the namespace import functionality within a workflow definition or subprocess. The imported namespaces apply to all Code activities in the workflow or subprocess after they have been added. Further to this, Workflow allows you to add references from other assemblies.

Parameters

The Code activity parameters consist of subprocess and workflow parameters that have already been configured. To use elements of the workflow (for example, parameters, local variables, activity properties, and so on) in your code snippet, you must add a parameter and link it to the desired data. Changes to a parameter's value are reflected in the workflow after the code has been executed. For example, the parameters are 'ref' or 'InOut'.

Example Code Snippet

In this example, there are subprocess parameters called Field1 and Field2. In the Code activity configuration, Field1 will be bound to the parameter called InitialValue. Field2 will be bound to a parameter called AdjustedValue.

AdjustedValue = InitialValue * 2 + 40;
If (AdjustedValue > 100) Then
                AdjustedValue = 100
End If

Upon running this script in an instance of a workflow, Field2 will be set to the value of AdjustedValue.