In this Connected Learning Path, we will create a Chain that illustrates how to dynamically account for a leap year.
Primary Learning Objective | To highlight the power of Variable Transformation for date operations including parsing, formatting and performing math-based operations |
Secondary Learning Objectives | To highlight the use of Runtime Inputs, Handlebars, and JSON parsing in Variables |
Prerequisites | Configure Handlebars Connector |
Supporting Template | CLP | Variable Transformation |
Step 1: Create a Chain
- From the Chains Module within your Workspace, select the DEV Environment
- Add a new Chain
- Name the Chain, CLP | Variable Transformation | Parsing Date and Time.
- Add Description: The Connected Learning Path Chain template illustrates how a Runtime Input for the Month and Year can dynamically generate various period key formats and account for leap years. This Template uses Variable Transformation to reformat dates as well as calculate the ending period key while dynamically accounting for leap years.
- Save the Chain
Step 2: Add a Runtime Input
Runtime Inputs illustrate how a Chain can prompt for values at execution. This is a valuable way to make Chains able to be used across multiple processes.
- Create the Command
- Add a Runtime Inputs to the Start node
Use the Search bar to search for "runtime". Then double-click to auto load the Command into the Start circle. - Click Add Input and select DropdownField.
- Configure the DropdownField with the following:
Display Name Month Required Checked Description Select the current month - Click Add Option
- Configure with the option with the following:
Value {"display":"January","value":"01-01"} Display Name January Default Not Checked
- Add a Runtime Inputs to the Start node
- Complete the Dropdown Field
- Add options for the remaining eleven months of the year. Update the Value and Display Names accordingly
- Example: {"display":"February","value":"02-01"}
- Add options for the remaining eleven months of the year. Update the Value and Display Names accordingly
- Add a NumberField Runtime Input
- After completing the Dropdown Field input, add a NumberField Input
- Configure the NumberField with the following:
Display Name Year Required Checked Description Enter the current year in YYYY format Default Value <leave blank> Min Value (Checked) 2000 Max Value (Checked) 2050
- Save the Runtime Input
Step 3: Add a Command to Create a Period Key
We use the Handlebars Connector to take the values specified in the Runtime Inputs and create a period key in YYYY-MM-DD format.
- Add a Render Text Template Command from the Handlebars Connector to the Chain
- Connect the Start Node (Runtime Inputs) to the Render Text Template Command
- Edit the Command
- Name the Command: Render Text Template - Create Period Key
- In the Template parameter, enter the following:
{{Year}}-{{Period}} - In the Variables section, add two variables by clicking the Add button twice on the Variables section
- For the first variable, specify the Variable Name as Year
- Note: Variable names are Case-Sensitive
- Click in the Value field
- In the Variable pane, expand Trigger ➡ Runtime Inputs and select Year
- In the second variable, specify the name as Period
- Click in the Value field
- In the Variable pane, expand Trigger ➡ Runtime Inputs and select Month
- Click on the Month variable (as denoted by the bubble) that was assigned to the value
- Click the Select transformation dropdown
- Select Parse text as JSON
- Click the plus (+) symbol
- Click the Select transformation dropdown again
- Select Get Value from JSON
- Click the plus symbol
- In the Get Value from JSON ,
- From the Output column's dropdown, select "T"
- Note: The T is the month variable that was established in the prior step
- In the Value column's field, enter "value" and press the Enter key
- Note: The text value must be in lower case and will appear as a grey bubble after pressing the Enter key
- From the Output column's dropdown, select "T"
- Click Accept
- Save the Command
Step 4: Add a Command to Perform Date Operations
We use the Handlebars Connector to perform several date operations including date math and reformatting.
- Add a Render Text Template Command from the Handlebars Connector to the Chain
- Connect the Render Text Template - Create Period Key Command to the Render Text Template Command
- Edit the Command
- Name the Command: Render Text Template - Date Parsing
- In the Template parameter, enter the following:
Time Period Selected:
Month: {{Month_Name}}
Year:
Period Key: {{Period_Key}}
End of Month Period Key: {{EOM_Key}}
-
- In the Template parameter section
- Click after Year
- Then expand Trigger ➡ Runtime Inputs and select Year
- In the Template parameter section
Step 5: Variables section
- Add three variables by clicking the Add button on the Variables section
- For the first variable:
- Specify the first Variable Name as Month_Name
- Click in the Value field
- In the Variable pane, expand Trigger ➡ Runtime Inputs and select Month
- Click the Month variable's bubble in the Value field
- Click the Select transformation dropdown
- Select Parse text as JSON, and click the plus (+) symbol
- Click the Select transformation dropdown again
- Select Get Value from JSON, and click the plus (+) symbol
- In the Get Value from JSON line, select T from the Output dropdown
- In the same Get Value from JSON line, enter display in the Value field and press the Enter key
- Click Accept
- Select Get Value from JSON, and click the plus (+) symbol
- Click the Select transformation dropdown
- Click in the Value field
- Specify the first Variable Name as Month_Name
- For the first variable:
- For the second variable:
- Specify the Variable Name as Period_Key
- Click in the Value field
- In the Variable pane, expand Render Text Template - Create Period Key
- Select Rendered Text
- In the Variable pane, expand Render Text Template - Create Period Key
- Click the Rendered Text variable's bubble in the Value field
- Click the Select transformation dropdown and select Parse Date / Time, and click the plus (+) button
- In the first dropdown box, enter %Y-%m-%d and be sure to press Enter
- In the Output date format field, enter %m/%d/%Y
- In the Input date timezone dropdown, type UTC and then select the (UTC) Dublin, Edinburgh, Lisbon, London timezone
- Repeat the UTC selection for the Output date timezone field
- Click Accept
- Click in the Value field
- Specify the Variable Name as Period_Key
- For the third variable:
- Specify the Variable Name as EOM_Key
- Click in the Value field. In the Variable pane, expand Render Text Template - Create Period Key and select Rendered Text
- Click the Rendered Text variable that was assigned to the value
- Click the Select transformation dropdown and select Add / Subtract Date and Time, and click the plus (+) symbol
- In the Format dropdown box, enter %Y-%m-%d and be sure to press Enter
- In the Operation dropdown, select Add
- In the Amount field, enter 1
- In the Unit dropdown, select Month
- Add an additional Add / Subtract Date and Time transformation
- In the Format dropdown box, enter %Y-%m-%d and be sure to press Enter
- In the Operation dropdown, select Subtract
- In the Amount field, enter 1
- In the Unit dropdown, select Day
- Output date timezone field
- Click Accept
- Click the Select transformation dropdown and select Add / Subtract Date and Time, and click the plus (+) symbol
- Specify the Variable Name as EOM_Key
- Save the Command
Step 6: Test the Chain
Now that the Chain is complete, we can test the result.
- Publish the Chain
- Click Execute and then select Run With Inputs
- From the Month dropdown, select February. In the Year input, enter 2024
- Click Start. Review the results of the Chain on the Log tab of the Render Text Template - Create Period Key Command
- Verify the End of Month Period Key i.e. a leap year
- Execute the Chain again using February but enter 2025 for the Year for this execution
- Confirm that the month ended on the correct day i.e. not a leap year
To learn more about data transformation using Chains, check out the Connected Learning Paths - Transformation Introduction!