# Commonly used Actions

Block

The Block action allows the grouping of elements in logical units. It is usually used to assign a list of the same operation to a higher-level unit.

![Use of Block statements to separate a script into logical units](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8HTRblOKav_qm4KEx6%2F-MEX09gpKXhRN3qu6AVB%2F-MEX1S90jTR0nQw3VvLH%2FScripting_Action_Blocks.png?alt=media\&token=c425ccaf-047e-4ca9-838e-aad7f3de66e6)

The example above has a Block element in which variables are assigned and another Block element in which operations are performed with those variables.&#x20;See also [Architecture of a Script](https://docs.tivity.one/building-an-application/execution/scripting/..#architecture-of-a-script) for more explanation.

Variables

Variables are commonly used to handle flexible script execution. A variable can be defined as a constant value or might hold the return of a called function. During script execution variables can be used in other actions.

### Example 1: Create a variable and assign a constant&#xD; value

In the Scripting Area click into the *Drop statement here* block and enter `variable` in the appearing search box. The Action menu is going to show you the *Variable* action. Select the Action to add it to the script.

Next, enter a *Name* for the newly created statement, e.g. `FY2021Begin` to indicate the begin of the financial year 2020/21. That name value is going to be listed in the Variables Menu and allows easy reference and use at a later stage in the script.

Then click into the *Expression (Any)* area to specify the data type of the variable, e.g. `DateTime`. The search feature assists you to find and choose the data type.&#x20;

Last, provide a value for the variable to complete the statement, e.g. `01.07.2020`.&#x20;

![Create a constant variable in the Script Designer](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8HTRblOKav_qm4KEx6%2F-MEWQLTDvdLnAwQOrgQ_%2F-MEWlvJjx2S-yKGIQSc6%2FScripting_Action_SetVariable.gif?alt=media\&token=42df7c7f-c694-4695-86e7-40bd92d25f5b)

The newly created variable appears in the Variable Menu on the right-hand side of the Script Designer as soon as the data type in the Expression field has been specified.&#x20;The variable can be used for other operations in the script.

### Example 2: Create a variable and assign an object&#xD;

In the Scripting Area click into the *Drop statement here* block and enter `variable` in the appearing search box. The Action menu is going to show you the *Variable* action. Select the Action to add it to the script.

Next, enter a *Name* for the newly created statement, e.g. `Context` to indicate the runtime context of the script. The variable is going to be listed in the Variables Menu and allows easy reference and use at a later stage in the script.

Then click into the *Expression (Any)* area and enter `getinstance` in the appearing search box. The result list is going to show you the *GetInstance* action which has a return type of `Object`. The GetInstance action is normally used in an instance workflow. As a reminder: “An instance workflow is a workflow that is called-up for a specific object.” The “Context” part refers to this object.

Finally, drag and drop the corresponding, built-in variables from the Variables Menu into the properties of the GetInstance expression to complete the newly created variable.

![Create a variable using the GetInstance action in the Script Designer](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8HTRblOKav_qm4KEx6%2F-MEWmwZMiDqZ4RN2ke7n%2F-MEWs7U1wrvFYBGIJ898%2FScripting_Action_SetVariableGetInstance.gif?alt=media\&token=7ee4b0de-3fe5-4587-9320-dbdb5dd27740)

An Object returned by GetInstance action is identified by the following fields.

| Property             | Description                                                                                                     |
| -------------------- | --------------------------------------------------------------------------------------------------------------- |
| OfficeId (Guid)      | Indicates the ID of the workspace in which the application is located and for which a workflow is created.&#xD; |
| ApplicationId (Guid) | Indicates the ID of the application located in the selected workspace.&#xD;                                     |
| ClassId (Guid)       | Indicates the ID of the class that identifies the object.&#xD;                                                  |
| InstanceId (Any)     | Each object has a unique ID. To reference a specific object, the specific ID can be entered here.&#xD;          |
| LinkPath (LinkPath)  | The LinkPath is the navigation path to an object. It describes the steps that led to the object.                |

### Example 3: Reference a variable&#xD;

After creating variables and assigning values or objects to them, it is time to work with those variables. To illustrate this, take the subtraction of two numbers.

First define two variables `Number 1` and `Number 2` as described in [Example 1: Create a variable](#example-1-create-a-variable-and-assign-a-constant-value).&#x20;

Then add the *Subtract* action to the script and drag the two variables from the Variables Menu on the right-hand side to the desired place in the statement.

![Get values of variables in an action](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8HTRblOKav_qm4KEx6%2F-MEX2PoIWCIrzZun2b9J%2F-MEXCLhJRyWq3WBwydT1%2FScripting_Action_GetVariable.png?alt=media\&token=a9b4754b-31d7-45e7-beca-e54c5062e2fe)

{% hint style="info" %}
The choice of `Get` or `Set` operation defines how the value of a variable is used in a script (see green boxes above).
{% endhint %}

GetCurrentUser

Many scripts require information from the user who initiated the script, i.e within a workflow. In order to interact with the user on the TIVITY platform get the user object using the *GetCurrentUser* action and save it in a variable called `User`.&#x20;

{% hint style="info" %}
A user is uniquely identified by an office. \
Therefore select the *Context.OfficeId* variable from the Variables Menu.
{% endhint %}

![Get the current user and store it in a variable](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8HTRblOKav_qm4KEx6%2F-MEXNXQmMFSN5XEVeyBG%2F-MEXOGb2urxvnqZQCgKh%2FScripting_Action_GetCurrentUser.png?alt=media\&token=e248cdaa-1914-4ff0-8413-46e118ea1f30)

Now, it is possible to work with the user object just like with any other object and, i.e. use the *GetValue* action to read out a user’s ID.

## Working with Values

Compared to variables Values are associated with an object. The two actions `GetValue` and `SetValue` allow you to read a field of an object and to define a new value for a field.

### GetValue&#xD;

The *GetValue* action allows to read a value from a field of an object.&#x20;The action has two parameters. *Object (Object)* identifies the object from which the value is to be read. *Field (String)* is the property of the object which holds the value. The terms in parentheses indicate the expected data type.

![Read the Field (Id) of an object](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8HTRblOKav_qm4KEx6%2F-MEXH5RNhmqVCfrhA6By%2F-MEXGqwJm-ZENqKl1jdE%2FScripting_Action_GetValue.png?alt=media\&token=8c939196-a4f1-446d-950f-999a3474d129)

The example above uses the *GetValue* action from the Actions Menu. Then, drag the “Object” object previously defined from the Variables Menu into the *Get Variable* area. Next, enter the word “Id” in the text box next or under Field.

{% hint style="info" %}
Field is case-sensitive, i.e. upper case and lower case are important here.
{% endhint %}

Field denotes the field of an object. This means that it is possible to look up the key of a field in an object in the class designer. An alternative way to access a Field is described under [GetSystemName](#getsystemname).

### SetValue&#xD;

In comparison the *SetValue* action allows values to be set in a field of an object. The two parameters *Object* and *Field* are equivalent to the GetValue action. The third parameter *Value* is the actual value to be stored in the field.

In order to be able to set a value to an object you need to hold a reference to it. The action *GetInstance* provides you access to such an object reference, as described in [Example 2: Create a variable and assign an object](#example-2-create-a-variable-and-assign-an-object).

![Set or update the value in a field (Description) of an object (Note)](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8HTRblOKav_qm4KEx6%2F-MEXHAJN3dvClipVGz44%2F-MEXLKl0vVs38Pv1JKX6%2FScripting_Action_SetValue.png?alt=media\&token=f6f6dc4d-6e0b-48c6-a790-ea1767855950)

In the example above you get a reference of an *Object* of type `Note` using a variable. Next, the script is going to update the *Field* `Description` with the information provided in the *Value* expression.

{% hint style="info" %}
This example was greatly simplified. In a real scenario, the user should be able to enter a value on the UI, and the script should represent the logic to store this value.
{% endhint %}

GetSystemName

The *GetSystemName* action allows you to look up the names of fields of a class directly in the Script Designer. This eliminates the need to go into the [Class Designer](https://docs.tivity.one/building-an-application/data/data-modelling) just to look up the name of a field.

<figure><img src="https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8HTRblOKav_qm4KEx6-887967055%2Fuploads%2FhhFhXGQbLRXesykQzcE2%2FGetSystemName_HowTo.gif?alt=media&#x26;token=b49817fb-5440-4f82-aa1a-42eb79353bb5" alt=""><figcaption><p>Use GetSystemName to reference a field and set a new value</p></figcaption></figure>

The example shows how to set a value to a variable `Note` of type Object. It uses the *GetSystemName* action to reference the field `Description` of an object (here: Unit).

## GetSystemId

Just as with the *GetSystemName* action, the GetSystemId action allows you to fetch the Ids. A list of available classes, fields, actions, workflows etc. can be used to search for a specific name. If you don't know the name, you can also search for the type, e.g. with the search word 'class' all classes are listed.\
In addition, if you enter e.g. 'controller', all controllers will be listed.

![Script helper GetSystemId](https://2608258621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8HTRblOKav_qm4KEx6-887967055%2Fuploads%2F4ik3TiWscvQ4yijoESom%2FGetSystemId.png?alt=media\&token=57e61c15-1218-499b-917d-065f71c34264)

#### Available ID lists

* Class
* Field
* Action
* Workflow
* Layout
* Query
* Script
* Template
* Function
* Link
* LinkServiceType
* Controller
* Group
* Office
* Feature
