# ETL functions execution sequences overview

The "Code" tab contains functions where users can define actions for the ETL pipeline.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlcodetab.png" alt=""><figcaption></figcaption></figure>

## Sequence of Functions

The ETL pipeline executes these functions in the "Code" tab in the following order:

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlexecflowch.png" alt=""><figcaption></figcaption></figure>

## On Starting

When the ETL pipeline is run, the first function to be executed is the “On starting”. This function user can define the actions to be performed as soon as the ETL pipeline is initiated. On running the ETL pipeline, the relevant servers initiate all the services for the ETL pipeline and then perform the actions defined in this function as soon as the service is about to initialize the data source and data destination.&#x20;

## On Reader initializing

After the “On Starting” function is processed, the “On Reader initializing” function is executed. The Reader is initialized, which finds and opens the relevant source to read the data from.&#x20;

## On Writer initializing

The actions in the “On Writer initializing” function are executed once the Reader is initialized. The Writer is initialized, which finds and opens the relevant file or target entity to write the data.

## On Reading record

When the ETL pipeline Reader and Writer are initialized, the data is ready to be read from the source when the first record is read the “On Reading record” function is executed.&#x20;

The “On Reading record” function can apply the actions to handle any adjustments to the data after it has been read from the source.&#x20;

On clicking \[Show Shared Variables] in On Reading record>Action box>Variables, three variables are visible other than the StartupParameters variable: “reader,” “skippedReason,” and “skipRecord.” These variables are within the scope of the “On Reading record” function, and, therefore, available to all Action boxes within the “On Reading record” function.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlorrsharedvar.png" alt=""><figcaption></figcaption></figure>

* “reader” of the data type “Record”: This variable contains the details of the record as defined with the fields present in the Data Format tab>Reader>Source fields.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlorrreadervar.png" alt=""><figcaption></figcaption></figure>

* “skippedReason” of the data type “string”: This variable contains the reason for skipping a record (if any) defined by the user.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlorrskipreasonvar.png" alt=""><figcaption></figcaption></figure>

* “skipRecord” is of data type “boolean”: This variable allows the user to skip the record and prevent writing it to the target destination by updating this variable to True. By default, it is set with False.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlorrskipdrecvar.png" alt=""><figcaption></figcaption></figure>

## Field Mapping completed

The field mapping defines the values read from the data source to where they need to be placed in the destination. Once the data is read from the data source, and the “On Reading record” function is executed, the relevant source fields are mapped to the fields in the destination. Once the data fields mapping is applied, the record is ready to be stored in the relevant destination.

## On Writing record

The “On Writing record” function is executed after the “On Reading record” function, and the field mapping is applied. In the “on Writing record” function, the record is ready to be written in the target entity. Users may define actions to update the field values of the mapped records. The values in the mapped records will be used when the record is written to the destination.&#x20;

In the scope of “On Writing record” function three variables are visible and therefore are available to all Action boxes within this section. These are displayed by clicking \[Show Shared Variables] in On Writing record>Action box>Variables.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlowrsharedvar.png" alt=""><figcaption></figcaption></figure>

These variables are “mapped,” “skippedReason,” and “skipRecord.”

* “mapped” of the data type “Record”: This variable contains the details of the record as in the fields present in the Data Format tab>Field Mapping.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlowrmappedvar.png" alt=""><figcaption></figcaption></figure>

* “skippedReason” of the data type “string”: This variable contains the reason for skipping a record (if any) defined by the user.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlorrskipreasonvar.png" alt=""><figcaption></figcaption></figure>

* “skipRecord” of the data type “boolean”: This variable allows the user to skip the record and prevent writing it to the target destination by updating this variable to True, by default it is set to False.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlorrskipdrecvar.png" alt=""><figcaption></figcaption></figure>

## On Skipped record

The “On Skipped record” function defines the actions to be performed when records are skipped either in the “On Reading record” function or in the “On Writing record” function.&#x20;

In the scope of this function two variables are visible and, therefore, are available to all Action boxes within the “On Skipped record” section. These are displayed by clicking \[Show Shared Variables] in On Skipped record>Action box>Variables.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlosrsharedvar.png" alt=""><figcaption></figcaption></figure>

These variables are “isFromReader,” “Skipped,” and “skippedReason”:

* “isFromReader”: This variable is of the data type “boolean”. Users can use this variable to verify if the record is skipped in the “on Reading record” section or “on Writing record” section.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlosrisfromreadervar.png" alt=""><figcaption></figcaption></figure>

* “Skipped”: This variable is of the data type “record” and contains the details of the skipped record.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlosrskippedvar.png" alt=""><figcaption></figcaption></figure>

* “skippedReason”: This variable is of the data type “string” and includes the reason for the skipped record.

<figure><img src="https://media.langstack.com/documentation/media/images/code/training_manual/chapter_9/ch9etlorrskipreasonvar.png" alt=""><figcaption></figcaption></figure>

Based on the function that contains the actions defining records to be skipped, the function execution sequence gets affected as follows:

1. If the actions for record skipping are defined in the “On Reading record” when the first record is read, the functions execution sequence is as follows:&#x20;
   1. The “On Reading record” function is executed.&#x20;
   2. The “On Skipped record” function is executed immediately after the “On Reading record”.&#x20;
   3. The “On Writing record” function is executed.&#x20;
   4. The “On Reading record” function is executed for the next record, and the cycle continues.<br>
2. If skipping the record is set to True in the “On Writing record” when the record is read, the functon execution sequence is as follows:&#x20;
   1. The “On Reading record” function is executed.&#x20;
   2. The “On Writing record” function is executed.&#x20;
   3. The “On Skipped record” function is executed immediately after the “On Writing record”.&#x20;
   4. The “On Reading record” function is executed for the next record and the cycle continues.

## On Completed

The “On Completed” function consists of the actions to be performed when the service has completed i.e. when the last record of data has been processed and all the functions for the ETL pipeline have been executed.


---

# 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.langstack.com/welcome/get-started/learn-langstack/introduction-to-etl-pipeline/etl-functions-execution-sequences-overview.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.
