# How to listen for SFTP folder changes

This guide shows how a Conditional Trigger can be used to start a Process. In this guide a file named readme.txt is searched from a SFTP folder, and if it is there it is downloaded to Frends.  If the file is not found, the calling Process is not run. If the file is found and downloaded the calling Process is started and it will just delete the downloaded file.

## Prerequisites <a href="#prerequisites" id="prerequisites"></a>

You will need the following prerequisites to follow through with this guide:

1. Editor access to your Frends tenant to access the Process Editor View.
2. An Agent Group and Environment setup up to run Processes.
3. The Task called 'Frends.SFTP.DownloadFiles' imported to your Frends tenant.&#x20;
4. Source SFTP server. In this guide we will use the free <https://test.rebex.net/> server available for testing.

## How to set up Conditional Trigger for SFTP polling

A Conditional Trigger works by starting defined Subprocess periodically, e.g. once a minute. If that Subprocess then returns something other than `null` or empty string `""` the Process containing the Conditional Trigger is run. In other words, the start of Process is defined by a Subprocess and therefore we create that Subprocess and the last step is to create the actual Process.

You can learn more about Conditional Trigger [here](/reference/triggers/conditional-trigger.md).

To start with the Process creation, save the connection details to Environment Variables, and then create the Subprocess we use as condition to trigger a Process.

### Creating Environment Variables <a href="#creating-environment-variables" id="creating-environment-variables"></a>

To store login information and a file four Environment Variables are used, so they can easily be changed in the future:

* \#env.ConditionalTrigger.SftpAddress with value test.rebex.net
* \#env.ConditionalTrigger.SftpUsername with value demo&#x20;
* \#env.ConditionalTrigger.SftpPassword with value password (in secret field)&#x20;
* \#env.ConditionalTrigger.Filename with value readme.txt&#x20;

### Creating a Subprocess <a href="#creating-a-subprocess" id="creating-a-subprocess"></a>

A new Subprocess can be created by navigating to the Subprocess List View and clicking the Create new button.&#x20;

<figure><img src="/files/I34uQtVuMM3GdvnRTN0h" alt=""><figcaption><p>Overview of the Subprocess for triggering a Conditional Trigger.</p></figcaption></figure>

#### Add SFTP Task <a href="#add-ftp-handler-task" id="add-ftp-handler-task"></a>

The next step in creating a Subprocess is to add a Task that can fetch the file from the source SFTP server. We are going to use a task called SFTP.DownloadFiles. The Task can be used for fetching files from SFTP server.

In this example we are going to configure information under Source and Destination tabs in the Task's Parameters. In source the Task is configured to use credentials and addresses stored in Environment Variables. In addition file name is taken from Environment Variable. In destination only directory needs to be defined.

<figure><img src="/files/NKqdq3s57gevj3BTubL6" alt=""><figcaption><p>Using Environment Variables for SFTP details helps with managing the integrations later.</p></figcaption></figure>

#### Checking for found files

Exclusive Decision shape is used to determine if files have been read from SFTP server. The Task returns number on transferred files in SuccessfulTransferCount that can be used to check if files have been found and transferred.

<figure><img src="/files/J4SjWPatqJPHUuNnU04g" alt=""><figcaption><p>Example condition for checking if files were found.</p></figcaption></figure>

If files are read, filename is returned to parent Process. TransferredFileNames contains a list of names of transferred files. Here we know that only one file is transferred and want to pass on only its name, so name can be extracted from list by calling First()  method.

<figure><img src="/files/87vnUzboVgicx2qDwlqt" alt=""><figcaption><p>Getting the first file name of the transferred files for a return value.</p></figcaption></figure>

If nothing is found empty string is returned to parent Process, as it tells Frends that Process contain Conditional Trigger should not run, returning null would work here as well.

<figure><img src="/files/6FpMHyFxO2CfpHS0xz1d" alt=""><figcaption><p>Returning empty string will not trigger the Conditional Trigger.</p></figcaption></figure>

### Creating a Process <a href="#creating-a-process" id="creating-a-process"></a>

A new Process can be created by navigating to the Process List View and clicking the Create new  button.&#x20;

<figure><img src="/files/hJM9YWfDY11zNpagrNCI" alt=""><figcaption><p>Overview of the main Process using the Conditional Trigger.</p></figcaption></figure>

#### Add Conditional Trigger <a href="#add-conditional-trigger" id="add-conditional-trigger"></a>

Conditional Trigger works by defining Subprocess used as condition and how often it is run. &#x20;

1. Drag a Trigger shape to the Process Editor from the Toolbar.
2. Set the type of Trigger to be Conditional from dropdown in Parameters.
3. Subprocess created before can be selected from dropdown menu.
4. Poll interval in seconds  can be left to 60. This defines how often the Subprocess is executed.\
   ​

<figure><img src="/files/XJDcHSDYDO9OIqce2pU5" alt=""><figcaption><p>Setting Conditional Trigger up is as easy as selecting the Subprocess and selecting poll interval.</p></figcaption></figure>

#### Add delete Task <a href="#add-delete-task" id="add-delete-task"></a>

Next we add Task Frends.Files.Delete to delete local files. It will need folder and name of file being deleted. Filename can be taken from Trigger data as it has been passed from Subprocess.&#x20;

<figure><img src="/files/wK5Dw5UlfWy9IfsJ9976" alt=""><figcaption><p>Deleting the file in order to not handle it again next time.</p></figcaption></figure>

## Testing the Process <a href="#process-testing" id="process-testing"></a>

The Process should now be ready to run. Conditional Trigger will start polling SFTP server when the Process is activated, but before that is done the log level for the Process should be set to **everything** for a couple of minutes, so the execution of the Subprocess in Conditional Trigger actually gets logged. If logging is set to **Default** or **Only errors** the Subprocess only gets logged if there is an error inside of it.  As the readme.txt should always be found on SFTP server, the Process should be started once a minute if everything is working.

If you want to observe how Conditional Trigger works when a file is not found, and thus the Process is not started you can change the filename in #env.ConditionalTrigger.Filename to something else. If you want to observe what happens when an error occurs in Subprocess in Conditional Trigger, you can modify the destination directory to something random in the Subprocess to produce exception during the Task execution.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.frends.com/guides/development/how-to-listen-for-sftp-folder-changes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
