In this tutorial, we will learn how to convert a list of comma-separated text values into a collection. Each item in the list will be transformed into an individual text item within the collection. This process is especially useful when you have saved answers from a Multiple Select input node in a database and wish to reuse them as default values. Follow along to discover the steps for converting these lists into collections effectively.
Transform a List of Comma Separated Text Values Into a Collection and Use It as a Default Value in Multiple Select Input Nodes
In this article, we assume that you read a comma-separated list of text values from a database General DB (in practice you will use your own database here) via a Read From a Database action. We assume that this list contains end users answers from a Multiple Select input node that have been saved to General DB earlier on in the process.
Our aim is to split the comma-separated list into individual text items and use those text items as default answers in a Multiple Select input node.
Rebuild example
- Create a data view by selecting the database (in our example, we call it General DB) where the comma-separated list is saved. Add an action to start the module you will be building below. Create a new parameter and change the Key to record_id. See for a detailed explanation here.
- In the module in which you want to use the comma-separated list to get the default values, add a URL Parameter action and type record_id into the QUERY PARAMETER NAME field. Add this action on top of the Multiple Select input node for which you want to use the values from the comma-separated list as default values.
- Add a Read From a Database action and name it Database Read. Connect it to the database in which the comma separated value has been saved (in our example, we call this database General DB). Reference the URL parameter in the ID field.
Now, proceed with the first step of the actual tutorial.
Tutorial
Here is how to proceed:
- Create a Custom action with the code provided below. If you are unfamiliar with creating Custom Actions, we recommend visiting the developer website.
- Add the Custom action right below the Read From a Database action that reads the comma-separated list providing the default values. Reference the database field which holds the comma separated values to the input field).
- Use the collection from the Custom action as the default values for the Multiple Select input.
MetaData{
Custom Code
"name": "Default Values Multi-Input",
"description": "This custom action enables the module to read a comma separated value then convert it to a collection.",
"key": "converting-into-collection",
"developerEmail": "gundogdu@bryter.io",
"parameters": [{
"name": "Comma Seperated Value",
"type": "text",
"description": "Please referance the comma seperated value"
}],
"result": {
"type": {
"type": "collection",
"items": "text"
},
"description": "Collection"
}
}function main(commaSeparatedValues: string): string[] {
return commaSeparatedValues.split(", ");
}
Disclaimer
❗ Please note that if an option includes a comma within its value, this custom code shall not function as expected.