# Code Task

Code Task shape can be used to run custom C# code, to assign a value from a C# statement to a variable or modify existing data in objects or elsewhere. Most anything you can do with C# programming, you can do in Process using Code Task.

## What is Code Task?

Code Task shape, also known as Code Block or C# Statement shape, allows execution of custom C# code in Frends Processes. The code follows the styling of an anonymous function, that either simply performs operations without explicitly returning a value like a void function, or returns a value that will be saved to a variable, like the Assign Variable shape does.&#x20;

<figure><img src="https://content.gitbook.com/content/5LGDIGALBToaaFjbtrP5/blobs/wiNZyCtSEVKDpswXWrUP/image.png" alt="Image shows Code Task being used to map a response in C# code."><figcaption><p>Even complex C# code can be included in a Code Task.</p></figcaption></figure>

Unlike Assign Variable shape, more than one line or expression of C# code can be used here, without clearly defined upper limit on the amount. It essentially fulfils the role of a Task more than the Assign Variable, being able to do many of the things a simple custom Task could do.

## Usage

To use Code Task, add it to Process canvas and connect it to your Process, alongside other shapes, between a Trigger and a Return or Throw shape. After adding it to your Process, you can give the shape a display name, as well as define the parameters.

Most important parameter to define for Code Task is the C# expression. Optionally you can also make the shape create a variable for Process, like Assign Variable shape does by default, and define a type for the variable, such as string, JObject or List\<string>. Any C# primitive type or class from included libraries can be used. If data type is not defined, the type will be dynamic and thus automatically deducted at runtime.

[You can learn more about using C# in Frends from here](https://docs.frends.com/reference/frends-6.1.0/process-development/c-in-frends).

Code Task is most commonly used for performing a single, well-defined operation within the Process, such as performing data mapping or filtering, using C# code. While a lot of functionality of Processes could be built into a single Code Task, it's recommended to keep the contents of a single Code Task at one activity per shape.&#x20;

Another way of looking at when to use Code Task and when to build the logic as a Process using different shapes, is whether the activity you need to perform is a single step of the business process or core aspect of the process that should be understandable when looking at the business process at top level.

### AI Code Assistant

In case you need to perform complex logic within your Process, you might prefer to use Code Task shape to write the logic out in C#. This is useful for performing complex mapping or analysis into the data within a Process. To speed this up, the AI Assistant is available here as well to give you a functional code snippet or a starting point to continue from where the AI took you.

<figure><img src="broken-reference" alt="Image showing the AI Code Generator in action."><figcaption><p>The AI Assistant will provide you with functional code that will work directly within Frends Code Task shape.</p></figcaption></figure>

Because writing C# code within Frends's Code Task has some special requirements that separate it from standard C# program code, the AI can help you in getting started with writing code suitable for embedding into the Process. It understands the namespace requirements as well as knows what libraries are available within Frends, and uses the Frends variables correctly to handle reference values from the Process as needed.

## Configuration

To set up a Code Task shape, add it to your Process canvas and connect it to your Process flow using Sequence flows. After this, you can configure the shape with your C# expression and optionally variable's name and data type.

The following configuration parameters exist for Code Task shape.

### Display name

Shapes can be given a display name in Frends Processes to distinquish them from each other. Shape's display name has no technical purpose and is only for documentational and visual purposes only. For shapes returning a #result reference value, the display name can be used to specify which result is meant.

Display name is given to the shape by double-clicking it on the canvas or through its configuration parameters.

### Assign Variable

Toggle option to select if the result of the C# statement should be saved to a variable. If disabled, the C# expression will be executed, but it's result value will not be saved to a variable, which can be useful for modifying existing objects.&#x20;

If enabled, allows the developer to specify a name for the variable, while requiring the C# code to use return statement to save a value to the variable.

### Statement

The C# code to execute for the shape. Can include as much C# code as required, in the form of an anonymous function. If result is to be saved to a variable, it must include return statement.

Alternatively, the input field's type can be changed to text, JSON or XML to assign the variable a string value, with syntax highlighting for the selected format. Handlebars syntax can be used within the plain text input to include C# expressions.

### Type

Text input field to specify the type of data for the variable. Any valid C# primitive type or class from included libraries can be given here to enforce the data type and simplify the variable's usage.

Alternatively the field can be left empty, resulting in dynamically typed variable, of which data type will be deducted at runtime.

### Skip logging result and parameters

Toggle option to select if the values of this shape should be included in logging or not. Default is false. If set to true, Process Instance logs will show the result and parameter values of this shape as **<< Omitted >>**.

### Dispose at the end of the scope

Toggle option to select if the result and other memory allocations should be released after the containing scope exits. The containing scope can be the Process execution itself, Scope shape, or a single iteration of Foreach or While Scope shape.

Can be useful if the shape results in a large amount of data, allowing its early release from memory if it's not needed later in the Process.

### Promote result as

Toggle option to select if the result value of this shape should be promoted in the Process Instance list, and an text input field if set to true. Default is false. Promoted values appear in the Process Instance list in their own column along with error and result values. Promoted value column is named with the text input field appearing when this is set to true.

Promoted values are always logged fully in the Process Instance view, regardless of the log level settings.&#x20;

## Reference Values

Code Task shape primarily provides a #var reference value if assign variable option is enabled, corresponding to the variable assigned within the shape.&#x20;

In addition, the shape provides an unnamed #result reference value, which also contains the result of the expression in the shape, in case it resulted in a value being returned or assigned to a variable. If the statement did not result in a value being returned, simple "code element executed" string will be stored instead.

### #var

Representing a variable in Frends Processes, #var reference values can be created as Process Variables from the Process's general parameters, or during Process flow using Assign Variable or Code Task shapes with Assign variable parameter enabled.

All methods to create variables in Frends Processes require defining a name for the variable. The variable can then be used as #var.MyVariable, if MyVariable is set as the variable's name.

### #result <a href="#result" id="result"></a>

\#result reference value can be used to access earlier shape's return value. For Scope shapes, the value is specified using Return shape, while for Task's the #result is populated directly by the Task.

<figure><img src="https://content.gitbook.com/content/5LGDIGALBToaaFjbtrP5/blobs/Q0PRpb7Zy1bPRUSq2Gga/image.png" alt="Example of result reference value being used to return a value from Process."><figcaption><p>Result reference value used to return the Task's result as response from the Process.</p></figcaption></figure>

When Return shape is used to end Foreach or While shapes, the expression or HTTP content values in Return shape are available through #result reference value as an array of values for each iteration.

\#result reference value by default references the last Task's or Scope shape's return value. Instead of unnamed #result reference, you can also use named #result reference value.
