Appending CSV File Using String Builder

Last Updated: 07/19/2016 Introduced in Verision: 2.0

This tutorial demonstrates how to append new rows to the CSV file using String Builder steps. These steps can be found in Data > Text > Builder category in the ToolBox.

 

Example:

We begin in the Designer Folder with clicking Create Flow on the Folder Actions panel.

 createFlow

Then, we Name the Flow and click Create to proceed to the Flow Designer.

 nameTheFlow

First, we need to create Data that will hold our CSV file. We add Create Data step from Favorite Steps category in the Flow Designer Wizard.

 addCreateDataStep

In the resulting pop-up we click Add New link to add new Data Definition.

 addDataDefinition

In the Add DataDefinitions window we Name our Data and define its Type FileData. Then we click Ok to save and close this pop-up window.

 nameDataDefinition

When we finished defining our Data we click Add in the Data Definitions window to continue.

 dataDefined

To use Text Builder steps in the Flow we need to Create a String Builder first. We add Create String Builder step from All Steps [Catalog] > Data > Text > Builder category in the Flow Designer Wizard.

 createStringBuilderStepAdd

Because the Contents of our CSV file is of type Byte Array, we add Get String From Bytes step from All Steps [Catalog] > Data > Text category to convert CSV file Contents into String.

 addGetStringFromBytes

Next, we can pick a CSV file for our Create Data step. The CSV file we are going to work with in this Example looks as following…

 originalCSV

We select Create Data step on the workspace and Step Information Layer pops up. Then, we use Value picker for our OriginalCSVFile Data in the Inputs section to select the desirable file.

 addCSVFileToTheCreateData

Next, we need to Append the String from Original CSV File to our String Builder. We add Append Line step from All Steps [Catalog] > Data > Text > Builder category.

 addAppendLineOne

Then, to map data for the Get String From Bytes step we select Show Mapping Editor from step’s Options Menu.

 showMappingForGetStringFrom

In the Mapping Editor we expand OriginalCSVFile and connect its Contents to the bytes field. Click Ok to save and close Mapping Editor.

 mappingForGetStringFrom

Back in the Flow Designer we call Mapping Editor for Append Line step.

 showMappingForAppendLineOne

In the Mapping Editor we connect CreateStringBuilder_Output to the data field and GetStringFromBytes_Output to the lineToAdd field. Then save and close Mapping Editor.

 mappingForAppendLineOne

Next, we are going to create a simple Form for the user to be able to add new rows. We add Show Form step from Favorite Steps category in the Flow Designer Wizard.

 addShowForm

In the resulting pop-up window we Name the Form and click Create to proceed to the Form Designer.

 nameAddForm

This simple Form is designed as following… It has a Title implemented with Label component from the Data category. Then, it has a Text Block component from Data category for user to input new Rows. And, it has Done button, implemented with Button component from Actions category. When we finish designing our Form we can save and close Form Designer.

 formDesignAddRows

Next, we are going to add new rows from the Form step to the String Builder. We add Append Text step from the All Steps [Catalog] > Data > Text > Builder category in the Flow Designer Wizard.

 addAppendTextStep

Then, we Show Mapping Editor for this step.

 showMappingForAppendText

In the Mapping Editor we connect CreateStringBuilder_Output to the data field. Then, we connect NewRows outcome from the Form step to the textToAdd field. Click Ok to save and close Mapping Editor.

 mappingForAppendText

Back in the Flow Designer we add To Text step from the All Steps [Catalog] > Data > Text > Builder category in the Flow Designer Wizard. With this step we are able to retrieve Data from the String Builder in the form of String.

 addToTextStep

We select Show Mapping Editor option to map Data for the To Text step.

 showMappingToText

In the Mapping Editor we connect CreateStringBuilder_Output to the data field. Then, we click Ok to save and close Mapping Editor.

 mappingForToText

Next, we add Get Bytes From String step from All Steps [Catalog] > Data > Text category in the Flow Designer Wizard. With this step we are going to convert String output from To Text step into an Array of Bytes. Then we will be able to use this Array of Bytes as Contents for our New CSV file.

 addGetBytesFromString

We use Step Information Layer to map data to this step. For the str field in the Inputs section we choose Select Value Mapping type, and using Path picker point to the ToText_Output.

 mappingForGetBytesFromString

Then, we click Add Step in the Step Information Layer to add final step to the Flow.

 addStep

We add Show Form step from Favorite Steps category in the Flow Designer Wizard. This Form will serve for the user to be able to Download an updated CSV file.

 addSecondForm

In the resulting pop-up window we Name the Form and click Create to proceed to the Form Designer.

 nameSecondForm

This Download File Form is designed as following… It has a Title that is displayed with a Label component from the Data category. It has a Close Button control from the Actions category. Finally, it has File Download component from File Handling category. With this control user will be able to download the File that is mapped to this control. We save the Form and close Form Designer, when finished.

 downloadFileFormDesign

Back in the Flow Designer we connect Close outcome from Download File Form to the End Step in our Flow. Then, we select Show Mapping Editor from the Download File Form’s Options.

 showMappinForDownloadForm

In the Mapping Editor we use Build Data Mapping type for the DownloadFile control.

 buildDataForDownloadFile

Next, we connect GetBytesFromString_Output to the Contents field. Note, that we need to use .csv file extension in the FileName field. We can save and close Mapping Editor.

 mappingForDownloadForm

This completes our Flow. We click Debug Flow link on the top panel in the Flow Designer to test our Flow.

Note that in Decisions version 3.5 and above, you’ll need to click Test Flow to access the Debugger.

3.5 Test Flow Shot

 debugFlow

Our Form for adding new rows opens in the Debugger. We add new coma separated rows and click Done.

 addRowsDebugger

Then, our Download File Form opens in the Debugger. We click Download File…

 downloadFileDebugger

We confirm File Download process…

 saveFile

Next, we Name our file and save it.

 nameAndSaveFile

In the Debugger, Decisions informs us that the File is Saved.

 fileSaved

We click Close on the Download File Form…

 closeForm

Diagram Tab in the Debugger indicates that our Flow ran to the End Step with no errors.

 flowDebugged

We locate and open our New CSV file… Confirm that new rows of Data were added.

 newFileUpdated

 

 

 

Additional Resources