◷ Reading Time: 10 minutes
This tutorial shows how to load filtered data from the database and apply some rules to the data.
- How to connect to a Database
- Filter data on the database side
- Apply some rules to the data
We want to apply discount business rules to some data that are in the database.
The idea is
- a flow loads the data from the database by passing dateStart and dateEnd to the database query
- Then iterates on the loaded data, and sets discount for each individual car based on the below business rules
If Year is between 2012 and 2015 then Discount is 10% If Year is between 2015 and 2017 then Discount is 4% If Year is 2017 then Discount is 1% Otherwise no Discount
The idea is
- A flow loads the relevant amount of data from the database by passing dateStart and dateEnd input parameters. So not all the data is loaded.
- Using a Loop to iterate on the loaded data, and sets Discount for each individual car
New Generic Flow
Create a new project and add a Flow as below:
The above Flow below nodes:
- a Generic Flow
Add Flow Parameters
Add the below parameters to flow
- cars (Out): List of the cars read from the database
- car (Local): Individual car to set the discount
- dateStart (In, Integer): Start year to filter data
- dateEnd (In, Integer): End year to filter data
You need to
- Set connection string
- Add a query to load data
Connect string can be static inside the flow model or can be passed on by your application. In this example, we use the static method.
Click on the button in front of the property value, and the entry window will pop up.
Add the connection string to the database, and click the OK button.
In the database’s Properties Window.
In the Properties Window of the Database node, select the Queries property and Click on the editor button.
The Query collection manager allows you to add multiple queries. In this case, click on the Add button to add our select query.
In the right panel, select Command property and click on the editor … button
Write your query in the SQL editor. In this example, we are writing a parameterized query that filters data based on the date on the database side. Then click OK to close the editor.
You are back on the query collection manager screen. Select the Parameters property and click the editor button.
Add two-parameter. Name is the parameter name of the query. And Value is the value of the parameter. Add two below parameters. With Name to start and Value of dateStart. And with Name set to end and Value of dateEnd.
And you finally have the query ready:
In this example, we use a Decision Table for the above rules. To model the above rules:
In the Flow, Right-click on the Decision Table node, and select Create & Link Document.
The new document selector appears, Rename your Decision Table to DiscountDT.XML and press OK
To open the new document (Decision Table), in your Project Explorer double click on the file named DiscountDT.xml
And you will have an empty Decision Table as below:
Our Decision Table is going to set a Discount on a Car
- Define and Input Parameter called car for this decision table
- And we will have
- One condition (year ranges)
- One action (discount assignment)
The final DT is like the below:
Expressions settings are:
- Condition Year: car.Year
- Action Discount: car.Discount
The last step is to finalize the flow to iterate over the cars and pass them to the Decision Table. Now we are going to link the Loop node to flow parameters:
Select the loop and properties windows will be updated. We have to set up the Iterator properties.
In the loop properties, select List Source and press the editor button
Press Control+Space and the suggestion box pops up, select cars and press OK button
Do the same for Item Name and select car and you will have the below properties:
In the Flow, press F5, and the JSON Composer screen appears. There you can assign dateStart and dateEnd as below:
- dateStart = 2014
- dateEnd = 2017
or use Logic Run Template to run/ debug the project.
When the Flow is run, you can use the data viewer to explore your data. Before going to the Decision Table you can view the below values:
And after running the Decision Table containing the running business rules against the loaded data, you see the below result:
Setup Your Database
In this example, we use MsSql, Create a new database called Car-Insurance, and use the below scripts to set up the table and data:
Use the attachment at the end of the page to download the sample project.