# Script examples

## Example: Append new records to a table

{% hint style="info" %}
\#FlowScript #[Join](https://help.novacuraflow.com/6.12/development/flow-studio/table/table-functions#the-join-function) #[If statement](https://help.novacuraflow.com/6.12/development/flow-studio/statements#conditional-statement) #[For loop](https://help.novacuraflow.com/6.12/development/flow-studio/statements#loops) #[With](https://help.novacuraflow.com/6.12/development/flow-studio/table#update-row-the-with-keyword) #[Table concatenation](https://help.novacuraflow.com/6.12/development/flow-studio/table#insert-row-the-table-concatenation-and-operator) #[Create table](https://help.novacuraflow.com/6.12/development/flow-studio/table#create-table-the-empty-table-statement)
{% endhint %}

This scrip creates a new table and fills it with data from an already existing table, where one column will be updated with new data.&#x20;

#### **Input table:**

<div align="left"><img src="https://3419513391-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LtPTgZOYmpJka4jqs1v%2F-LvKcFxI-Bk57CCx-PPj%2F-LvKsar0BY_IPTcDV2eg%2Fbild.png?alt=media&#x26;token=4a70a245-7677-46b2-ba0c-e6b0162fe956" alt=""></div>

*DS\_periodDates*

| date       | dayStatus              |
| ---------- | ---------------------- |
| 2019-12-01 | Confirmed              |
| 2019-12-02 | Reported               |
| 2019-12-03 | 3 hours left to report |
| 2019-12-04 | 8 hours left to report |

*DS\_favorites*

| customerId | customerName | project | accountDate |
| ---------- | ------------ | ------- | ----------- |
| 3245       | abc          | 4565    | 2019-12-01  |
| 2456       | adc          | 5465    | 2019-12-01  |
| 5974       | acb          | 4564    | 2019-12-01  |

#### **Script:**

```fsharp
let generatedFavorites = table(customerId, customerName, project, accountDate, objversion, objid);

for recDate in DS_periodDates do

    if recDate.date < now() and (recDate.dayStatus like '%hours left to report' or recDate.dayStatus = 'Reported') then

        for recFavorite in DS_favorites do 

            let addedFavs = recFavorite with [accountDate: recDate.date];

            set generatedFavorites = generatedFavorites & addedFavs;

        done

    end

done

return generatedFavorites; 
```

**Description:**

1. A new table is created: *generatedFavorites* (row 1 in script below)
2. The script will loop for every row in table *DS\_periodDates* (3) where the date is in the future and the day status is like '%hours left to report' or 'Reported' (5)
   1. The script will loop for every row in table *DS\_favorites* (7) where the account date in *DS\_favorites* will be updated with the date from *DS\_periodDates* (9)
   2. The script adds the result from each loop iteration to *generatedFavorites* by concatenating the row with table *addedFavs* (11)
3. The script returns *generatedFavorites* (19)

#### **Output table:**

<div align="left"><img src="https://3419513391-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LtPTgZOYmpJka4jqs1v%2F-LvLE4W4YsFEzRaCNhaL%2F-LvLEbqq8vxwEa7GY91B%2Fbild.png?alt=media&#x26;token=56a9f831-26f7-4bb2-94cd-f8433fe3fc0e" alt=""></div>

*generatedFavorites:*

| customerId | customerName | project | accountDate |
| ---------- | ------------ | ------- | ----------- |
| 3245       | abc          | 4565    | 2019-12-02  |
| 2456       | adc          | 5465    | 2019-12-02  |
| 5974       | acb          | 4564    | 2019-12-02  |
| 3245       | abc          | 4565    | 2019-12-03  |
| 2456       | adc          | 5465    | 2019-12-03  |
| 5974       | acb          | 4564    | 2019-12-03  |
| 3245       | abc          | 4565    | 2019-12-04  |
| 2456       | adc          | 5465    | 2019-12-04  |
| 5974       | acb          | 4564    | 2019-12-04  |

## Example: Join in to a record

{% hint style="info" %}
\#FlowScript #[Join](https://help.novacuraflow.com/6.12/development/flow-studio/table/table-functions#the-join-function) #[If statment](https://help.novacuraflow.com/6.12/development/flow-studio/statements#conditional-statement)
{% endhint %}

This script joins values and assign the values to new variables.

#### Input variables:

<div align="left"><img src="https://3419513391-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LtPTgZOYmpJka4jqs1v%2F-LvGZ-t6frhXZpkoCp2j%2F-LvG_Gya6SdDf9L0St_A%2Fbild.png?alt=media&#x26;token=c74961dd-449f-4645-8d45-8f048185afdc" alt=""></div>

#### Script:

```fsharp
let criticalBugsNoTeamJoinedKey = '0';
let criticalBugsNoProgressJoinedKey = '0';
let criticalBugsNotStaredJoinedKey = '0'; 

if count(criticalBugsNoTeam) > 0 then

set criticalBugsNoTeamJoinedKey = Join(criticalBugsNoTeam.key,'%27%2C%27');

end

if count(criticalBugs) > 0 then

set criticalBugsNoProgressJoinedKey = join(criticalBugs.key,'%27%2C%27');

end 

if count(criticalBugsNotStarted) > 0 then

set criticalBugsNotStaredJoinedKey = join(criticalBugsNotStarted.key,'%27%2C%27');

end 

let joined= [criticalBugsNoTeamJoinedKey: criticalBugsNoTeamJoinedKey, criticalBugsNoProgressJoinedKey: criticalBugsNoProgressJoinedKey, criticalBugsNotStaredJoinedKey: criticalBugsNotStaredJoinedKey]; 

return joined;
```

#### Output variable:

<div align="left"><img src="https://3419513391-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LtPTgZOYmpJka4jqs1v%2F-LvGZ-t6frhXZpkoCp2j%2F-LvG_nRO-EpN1wQuJWfS%2Fbild.png?alt=media&#x26;token=a0dbf4b6-3ab9-4c6e-b02a-e1bfc588e23d" alt=""></div>

## Example: Convert a complex table into a simple table

{% hint style="info" %}
\#FlowScript #[Join](https://help.novacuraflow.com/6.12/development/flow-studio/table/table-functions#the-join-function) #[Loop](https://help.novacuraflow.com/6.12/development/flow-studio/statements#loops) #[For loop](https://help.novacuraflow.com/6.12/development/flow-studio/statements#loops) #[Create table](https://help.novacuraflow.com/6.12/development/flow-studio/table#create-table-the-empty-table-statement)
{% endhint %}

This script will create a simple table where for example join is used to join all components together in to one string so all values can be put in to one column.&#x20;

#### Input table:

The input table *blockerBugsFiltered* is a complex table with differnt levels of tables, records and simple values in the table.

<div align="left"><img src="https://3419513391-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LtPTgZOYmpJka4jqs1v%2F-LvGIMsYjXK3PL96dUrS%2F-LvGS58o6NcorI1MNZ_J%2Fbild.png?alt=media&#x26;token=1e2a4e35-8a0f-4d2f-a671-139e1f6bae5f" alt=""></div>

#### **Script:**

```fsharp
let blockerBugs = table (key, summary, description, priority, components, assignee, created);

for rec_ in blockerBugsFiltered do 

let joinComponents = join(rec_.fields.components.name, ', ');

set blockerBugs = blockerBugs & [key: rec_.key, summary: rec_.fields.summary, description: rec_.fields.description, priority: rec_.fields.priority.name,
components: joinComponents, assignee: rec_.fields.assignee.displayName, created: left(rec_.fields.created,10)];

done

return blockerBugs; 

```

**Description:**

1. A new table is created: *bockerBugs* (row 1 in script below).
2. The script will loop for every row in table *blockerBugsFiltered* (3).
3. Join the values in the *components* table (5).
4. Append new records to table *blockerBugs* (7).
5. The script returns the table *blockerBugs* (12).

#### Output table:

The output of this script will be a simple table including only simple values.

<div align="left"><img src="https://3419513391-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LtPTgZOYmpJka4jqs1v%2F-LvGIMsYjXK3PL96dUrS%2F-LvGTbSo48Sp1qj3be5N%2Fbild.png?alt=media&#x26;token=3cafc9fc-2bea-4b7b-be0f-0de211ef06fe" alt=""></div>
