Overview
The Create Field operator isn't just for creating fields. You can also use it to update existing fields. In this article, you'll learn to update key/value pairs in a table.
Imagine that the value for "employed" is "true" in every row of the table. But, you want to switch the value from "true" to "yes". Using a simple employed="yes" expression updates the value across every row. This is the first key to update.
The second key is "fullPartTime", which defines if the person works full-time or part-time. The 2 possible values are "partTime" and "full". But you want to change the value from "full" to "fullTime". Using a conditional expression makes this update easy. Here's what that looks like: fullPartTime=IF(fullPartTime="full","fullTime",fullPartTime).
Let's break this down. Remember, the format of conditional expressions is key=IF(condition,valueIfTrue,valueIfFalse).
The condition the value from "fullPartTime" to "full".
The valueIfTrue is "fullTime", so that "full" gets replaced with "fullTime".
The valueIfFalse is the key fullPartTime. Meaning, if the value of fullPartTime is not "full", leave it as-is. This way, the "partTime" values get left alone.
Here's how the completed use case looks in the Module Builder:
Here's how the completed use case looks in Express View, including a peek at the DevTools Console:
What You'll Learn
In this article, you'll learn to use the Create Field operator to conditionally update existing key/value pairs in a table.
What You'll Need
For this use case, you'll need:
1 Data Table component
1 Button component
1 Data Workflow component
For your Data Workflow, you'll need:
1 Input operator
1 Create Field operator
2 Console operators
Configure the Data Table Component
First, let's create the table where you add your key/value pairs.
Drag and drop a Data Table component onto your canvas.
Enter dtTable in the Label and Property Name fields.
name
incomeAnnual
employed
fullPartTime
Kris
36000
true
partTime
Yuki
98500
true
full
Graciela
74000
true
full
Click Save.
Configure the Data Workflow Component
With the Data Table set up, you're ready to use the Create Field operator. First, update the employed key/value pair in every row (object) in your table (array). Then, conditionally update the fullPartTime value from "full" to "fullTime".
Drag and drop a Data Workflow component onto your canvas, placing it below the Data Table.
Enter dwfCreateField in the Canvas Label Text and Property Name fields.
Configure the Input Operator
First, connect your Data Table to the Data Workflow using Input operators.
Drag and drop an Input operator onto your Data Workflow canvas.
Configure the operator's Info window as follows:
Setting
Value
Category
Input
Component
dtTable
Required
Yes
Source
Default
Configure the Create Field Operator
Next, let's set up the Create Field operator.
Drag and drop a Create Field operator onto your Data Workflow canvas.
Configure the operator's Info window as follows:
Setting
Value
Category
Create Field
Label
employment
Do Not Sanitize Formula
Yes (checked)
Field 1
employed="yes"
Field 2
fullPartTime=IF(fullPartTime="full","fullTime",fullPartTime)
Connect the output port (right) of the dtTable Input operator to the input port (left) of the Create Field operator.
Configure the Console Operators
For your last 2 operators you'll add Console operators. These operators provide an easy way to see the before and after of your Create Field operator.
You'll notice that the Create Field operator only outputs to a Console operator. When creating applications, you might also want to use a dedicated Output operator. Then, you can connect that Output operator to a Hidden component. That way, you can store the final table and reference it elsewhere in your application.
Drag and drop 2 Console operators onto your Data Workflow canvas.
Configure the first Console operator's Info window as follows:
Setting
Value
Category
Console
Label
Before Create Field
Connect the output port (right) of the dtTable Input operator to the input port (left) of the Before Create Field Console operator.
Configure the second Console operator's Info window as follows:
Setting
Value
Category
Console
Label
After Create Field
Connect the output port (right) of the Create Field operator to the input port (left) of the After Create Field Console operator.
Click Save.
Configure the Button Component
Finally, let's create a button you can click in Express View to trigger your Data Workflow.
Drag and drop a Button component onto your canvas, placing it below the Data Workflow.
Enter btnRunDWF in the Property ID field.
Enter Run Create Field in the Label Text field.
Select Event as the Action Type.
Enter dwfCreateField in the Trigger on Click field.
Click Save.
Save your module.
Now you're ready to test out your Data Workflow. Preview your module in Express View and open the DevTools Console. Click the Run Create Field button and see what displays in the Console. It looks something like this:
Lab
You can find the completed Updating and Conditionally Updating Key/Value Pairs in a Table lab here: https://training.unqork.io/#/form/5f6e53e8f1a98d024e964c85/edit.