Comment on page
CSV Writer Event
An overview of how to configure the CSV Writer event in Dittofi
The CSV writer event lets you build in functionality to export data to a CSV format out of your app and write it to a file. You can set up the CSV writer event in five quick and easy steps.
- Step 1. Create Export CSV Endpoint
- Step 2. Create Export CSV Action
- Step 3. Add a File Read Event
- Step 4. Add a CSV Writer Event
- Step 5. Build Code and Test
For the purpose of this example, we are going to be exporting data from inside a Data Model called "Addresses" which contains the following column headers, data types and data.
You can repeat the steps in this tutorial with whatever data set you're reading from your Data Models.
You can create an Export CSV Endpoint from inside the Export CSV Endpoints tab. To do this, head on over to the Export CSV Endpoint and click on
Next, set up the endpoint as below.
- The "Request Method" option can be anything here and the endpoint does not need any variables set, unless of course you're looking to export of specific subset of records from your system.
- The Res. Content Type (Response Content Type) is set to text/CSV.
Once you've filled in the endpoint values as above, save your Export CSV Endpoint and close it.
By itself your Export CSV Endpoint does nothing. To give it functionality, we need to create an Export CSV Action and associate that action with our Export CSV Endpoint. To create an Export CSV Action, head on over to the actions tab and create a new action by clicking on
Next, give your action the same name as your Export CSV Endpoint and set the Action Trigger to be the Export CSV Endpoint, as below.
Next, we need to add two events to our action. The first event is going to create a file on our server that we are going to write to and the second event is going to write data to that file.
The event that we need to use in order to create a file on our server is (rather unintuitively) the File Read Event. You can add this event to your action by clicking on the "+ Add Event" button below your actions and events graph.
The file read event has the following configuration:
For now, because we are looking to write data to a CSV file, let's configure the File read event in the following way.
Notice, because we are looking to write data to a CSV, we have created a variable called FilePath from inside our Export CSV Action. This variable has the following type, label, name and expression.
Next, save your File Read Event and add the CSV Writer Event. This is going to be used to write data to the file that we just created in the File Read Event, as shown below.
To write data to the file, first we must find the address of the file. This can be done by looking up the File Variable that we created in our previous event, as shown below.
The File Variable that we add above, is the file that we created as our As Variable when we added the File Read Event in Step 3 above.
Next, we set the With Header parameter which tells us if we want to include a header with all of the column names in the CSV file that we are about to write. This parameter can either be true or false and to set it, we need to first create a new variable of type Check Box from within our action.
And second, (if we want to include a header within our CSV file) we can map the trueCheckbox Variable to our With Header parameter as below.
Lastly, the Object Parameter is the actual set of information that we want to write to the file. As we mentioned at the start of this tutorial this is our Addresses Data Model, shown again below.
To access this data from within our CSV Writer Event, we need to first create a new variable called "Addresses" and of type Collection, as below.
Second, we can associate this variable with our CSV Write Events Object Parameter.
Save your CSV Write Event and then (lastly), set the response variable in your action to your File variable. This is going to be the file that you're writing to.
The final steps are to (1) build your code and (2) test your CSV Export Endpoint. To build your code, click on
. Once the code has built, you can test this from within your Export CSV Endpoint, as below.
Notice, the data that is returned from your Export CSV Action is contained within the Endpoint Response body, as shown below.
To actually see the file download, you need to take the URL path for your Export CSV Endpoint and copy it into another tab, as shown below.