CSV Writer Event
An overview of how to configure the CSV Writer event in Dittofi
Last updated
An overview of how to configure the CSV Writer event in Dittofi
Last updated
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
PLEASE NOTE
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.
Next, set up the endpoint as below.
NOTICE
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.
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:
Event Parameter | Description |
---|---|
File Path | The path of the file that we are going to read or in this case create. This can be passed in as a variable to our event. |
As | This is a new variable that represents the file that the File Event is reading from. |
Create New File | A check box that if check will allow the File Read Event to create a new file. |
Prevent creating if already exists | A check box that if checked will prevent you from creating a new file if there is already a file at the location set by your File Path. |
Write mode | There are two options here, Append or Overwrite. If you have Append set here, this will mean that when you open the file any information that we are about to write to the file will Append to the existing file. Alternatively, if you have Overwrite set here, this will mean that when you open the file any information that we are about to write to the file will Overwrite the contents of that file. |
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.
NOTE
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.
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.
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 .
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 .
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.